java的RandomAccessFile类


 

package src.bean;

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

class Student {
     private String name;
     private  int age;

     public Student() {
         super();
    }

     public  int getAge() {
         return age;
    }

     public  void setAge( int age) {
         this.age = age;
    }

     public String getName() {
         return name;
    }

     public  void setName(String name) {
         this.name = name;
    }

     public Student(String name,  int age) {
         super();
         this.name = name;
         this.age = age;
    }
}

public  class UseIO {

     public  static  void main(String[] args) {
        RandomAccessFile randomAccessFile =  null;
         try {
             //  创建一个随机访问文件对象,并设置为可读写模式
            randomAccessFile =  new RandomAccessFile("src\\bean\\newFile.txt",
                    "rw");
            System.out.println("文件指针当前位置:" + randomAccessFile.getFilePointer());

             //  添加内容到文件中去
            
//  使用writeChars方法把一串字符写到文件中
            
//  randomAccessFile.writeChars("I am here!If you love me,please give the kiss to me!\nThank you for your love!");

            
//  使用writeBytes方法把一串字符写到文件中,使用该方法将会被舍弃字符中的高8位,所以实际上写入的是字符中的低8位.
            randomAccessFile
                    .writeBytes("I am here!If you love me,please give the kiss to me!\nThank you for your love!");
            System.out.println("使用readLine方法读取数据:");

            System.out.println("此时文件指针当前位置:"
                    + randomAccessFile.getFilePointer());

             //  重新把文件指针定位到开始处
            randomAccessFile.seek(0);

             //  使用readLine读取文件内容,每个字节的值被转换为字符的低8位,而字符的高8位被赋予0.因此这个方法不支持unicode字符集.
            String content = randomAccessFile.readLine();
             while (content !=  null) {
                System.out.println(content);
                content = randomAccessFile.readLine();
            }

             //  使用read方法读取指定长度的字节
            
//  重新把文件指针定位到开始处
            randomAccessFile.seek(0);

             byte[] b =  new  byte[10];
             int length = randomAccessFile.read(b);
            System.out.println("真正读取的字节数:" + length);

             //  使用当前平台当前的默认字符集把字节数组转换为字符
            String convertStr =  new String(b);
            System.out.println("转换后的内容为:" + convertStr);

             //  使用skipBytes跳过若干个字节后,读取后面的字节
            
//  重新把文件指针定位到开始处
            randomAccessFile.seek(0);
            length = randomAccessFile.skipBytes(10); //  参数可以为负数,当为负数时,则该方法不起作用
            System.out.println("实际跳过的字节数:" + length);
            content = randomAccessFile.readLine();
             while (content !=  null) {
                System.out.println(content);
                content = randomAccessFile.readLine();
            }

             //  之前使用writeBytes写入内容,所以如果我们使用readChar读取内容中的一个字符时,可能将出错
            
//  出现乱码的原因在于,使用该方法将从文件中读取两个字节做为一个字符高8位和低8位,作为一个unicode字符
            
//  重新把文件指针定位到开始处
            randomAccessFile.seek(0);
             char c = randomAccessFile.readChar();
            System.out.println("读取一个字符:" + c);
            System.out.println("读取的这个字符的值为:" + ( int) c);

             //  设置文件的内容为0字节
            randomAccessFile.setLength(0);
             //  注意使用UTF格式写入字符串时,对于英文字符,则占一个字节,中文字符,占三个字节,
            
//  而且当使用writeUTF时,在文件的开始处将写入整个字节的长度(注意不是字符串长度),占两个字节
            randomAccessFile.writeUTF("我爱你!i love you!");
             //  重新把文件指针定位到开始处
            randomAccessFile.seek(0);
            System.out.println(randomAccessFile.readUTF());
            System.out.println("使用writeUTF方法写入字符串时,文件字节长度为:"
                    + randomAccessFile.length());

             //  设置文件的内容为0字节
            randomAccessFile.setLength(0);
             //  创建两个学生记录,并写入文件中
            Student[] studs =  new Student[] {  new Student("andy", 23),
                     new Student("lili", 22) };

             for (Student stud : studs) {
                randomAccessFile.writeUTF(stud.getName());
                randomAccessFile.writeInt(stud.getAge());
            }

             //  读取刚才写入的内容
            
//  重新把文件指针定位到开始处
            randomAccessFile.seek(0);
             //  创建学生记录:
            Student[] studCreated =  new Student[2];
             for ( int i = 0; i < studCreated.length; i++) {
                studCreated[i] =  new Student();
                studCreated[i].setName(randomAccessFile.readUTF());
                studCreated[i].setAge(randomAccessFile.readInt());

                System.out.println("第" + i + "位学生的记录:");
                System.out.println("name:" + studCreated[i].getName());
                System.out.println("age:" + studCreated[i].getAge());
            }
        }  catch (FileNotFoundException e) {
             //  TODO Auto-generated catch block
            e.printStackTrace();
        }  catch (IOException e) {
             //  TODO Auto-generated catch block
            e.printStackTrace();
        }  finally {
             try {
                randomAccessFile.close();
            }  catch (IOException e) {
                 //  TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

 

这个例子需要注意的是UTF对中文字符的处理以及使用writeBytes方法与writeChars方法的区别.
运行后的结果为:

文件指针当前位置:0
使用readLine方法读取数据:
此时文件指针当前位置:77
I am here!If you love me,please give the kiss to me!
Thank you for your love!
真正读取的字节数:10
转换后的内容为:I am here!
实际跳过的字节数:10
If you love me,please give the kiss to me!
Thank you for your love!
读取一个字符:?
读取的这个字符的值为:18720
我爱你!i love you!
使用writeUTF方法写入字符串时,文件字节长度为:23
第0位学生的记录:
name:andy
age:23
第1位学生的记录:
name:lili
age:22
此时newFile.txt中的内容为:andy lili


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM