JAVA——59.随机访问文件类randomaccessfile

【randomaccessfile】文件类,封装了流的对象,并提供了缓冲区(字符数组),指针(可以在缓冲区读取、指向某些特定地方的数据)。这个随机访问文件类既可以读,也可以写。
练习一、利用随机访问文件类randomaccessfile读取a.txt文件中的内容
方法一、反复执行相同程序

package org.zhaiyujia.pkg1;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class randomaccessfile {
    public static void main(String[] args) {
        try {
            RandomAccessFile raf=new RandomAccessFile("f:/a.txt","rw");//参数分别是文件位置和模式(rw:可读可写)
            String str1=raf.readLine();//readLine读出这行,指针指向下一行
            System.out.println(str1);
            str1=raf.readLine();
            System.out.println(str1);
            str1=raf.readLine();
            System.out.println(str1);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

这里写图片描述
方法二、while循环
这里写图片描述
方法三、字节数组

package org.zhaiyujia.pkg1;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class randomaccessfile {
    public static void main(String[] args) {
        try {
            RandomAccessFile raf=new RandomAccessFile("f:/a.txt","rw");//参数分别是文件位置和模式(rw:可读可写)
            //String str1=raf.readLine();//readLine读出这行,指针指向下一行
            //System.out.println(str1);
            //str1=raf.readLine();
            //System.out.println(str1);
            /*String str1=null;
            while((str1=raf.readLine()) != null) {
            System.out.println(str1);
            }*/
            long len=raf.length();//返回文件长度
            byte[] b=new byte[(int)len];//强行则断,实际开发过程中不能这么做
            raf.read(b);
            System.out.println(new String(b));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

这里写图片描述
练习二、通过指针的方法seek(),读指定位置后面的内容

package org.zhaiyujia.pkg1;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class randomaccessfile {
    public static void main(String[] args) {
        try {
            RandomAccessFile raf=new RandomAccessFile("f:/a.txt","rw");//参数分别是文件位置和模式(rw:可读可写)
            //String str1=raf.readLine();//readLine读出这行,指针指向下一行
            //System.out.println(str1);
            //str1=raf.readLine();
            //System.out.println(str1);

            long len=raf.length();//返回文件长度
            raf.seek(10);//指针指到第10个位置,即d前面一个位置l,再从d往后读取
            String str1=null;
            while((str1=raf.readLine()) != null) {
            System.out.println(str1);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

这里写图片描述
练习三、利用随机访问文件类randomaccessfile往a.txt文件中的内容中添加写“hahahaha”

package org.zhaiyujia.pkg1;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class randomaccessfile {
    public static void main(String[] args) {
        try {
            RandomAccessFile raf=new RandomAccessFile("f:/a.txt","rw");//参数分别是文件位置和模式(rw:可读可写)
            raf.seek(raf.length());
            raf.writeBytes("hahahaha");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/zhaiyujia15195383763/article/details/81286886