随机访问文件

  • RandomAccessFile类可以在文件中的任何位置查找或写数据。磁盘文件都是随即访问的,但是与网络套接字通信的输入/输出流却不是。打开一个随机文件,只用与读入或者同时用于读写,可以通过使用字符串r(用于读入访问)或rw(用于读入/写出访问)作为构造器的第二个参数来指定这个选项。
RandomAccessFile in = new RandomAccessFile("C:\\Users\\whz\\Desktop\\岗位要求.txt", "r");
RandomAccessFile inOut = new RandomAccessFile("text.txt", "rw");
  • 随即访问文件有一个表示下一个将被读入或写出的字节所处位置的文件指针,seek方法可以用来将这个文件指针设置到文件中的任意字节位置,seek的参数是一个long类型的整数,它的值位于0到文件按照字节来度量的长度之间。
  • getFilePointer 方法将返回文件指针的当前位置。
  • RandomAccessFile类同时实现了DataInputDataOutput接口。为了读写随机访问文件,可以使用readInt/writeIntreadChar/writeChar之类的方法。
public static void main(String[] args) {
		//默认编码
        System.out.println(Charset.defaultCharset());
        //获取姓名使用按UTF8编码(本人系统默认编码)还可以在()中指定编码
        System.out.println("xxx".getBytes().length);
        Employee[] staff = new Employee[3];
        staff[0] = new Employee("xxx",1394.1,1996,6,6);
        staff[1] = new Employee("xxx",2394.1,1997,6,6);
        staff[2] = new Employee("xxx",3394.1,1998,6,6);

		//使用wite方式将数据写入文件
        try(DataOutputStream out = new DataOutputStream(new FileOutputStream("employee.dat"))) 
        {
            //保存雇员记录进入employee.dat文件
            for(Employee e:staff){
                out.write(e.getName().getBytes());
                out.writeDouble(e.getSalary());
                out.writeInt(e.getHireDay().getYear());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
        //使用read方式将数据从文件中读出来
        try(RandomAccessFile in = new RandomAccessFile("employee.dat", "r")){
            for(int i = 0 ; i < 3 ; i++){
            	//一个中文占了三个字节
                byte[] chars = new byte[9];
                in.read(chars);
                String name = new String(chars,0,9);
                double salary = in.readDouble();
                int y = in.readInt();
                System.out.println(name+" "+salary+" "+y);
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }



        //使用·writeChars/readChar读取数据写入文件/从文件中读取数据
        //按照Unicode编码,直接计算字符串中包含字符个数即可
        /*try(DataOutputStream out = new DataOutputStream(new FileOutputStream("employee.dat"))) {
            //保存雇员记录进入employee.dat文件
            for(Employee e:staff){
                out.writeChars(e.getName());
                out.writeDouble(e.getSalary());
                out.writeInt(e.getHireDay().getYear());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
        try(RandomAccessFile in = new RandomAccessFile("employee.dat", "r")){

            for(int i = 0 ; i < 3 ; i++){
                char[] chars = new char[3];
                for(int j=0;j<3;j++){
                    chars[j] = in.readChar();
                }
                String name = new String(chars,0,3);
                double salary = in.readDouble();
                int y = in.readInt();
                System.out.println(name+" "+salary+" "+y);
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
      
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值