博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java的RandomAccessFile类
阅读量:4682 次
发布时间:2019-06-09

本文共 4198 字,大约阅读时间需要 13 分钟。

 

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

转载于:https://www.cnblogs.com/jhxk/articles/2703622.html

你可能感兴趣的文章
open_basedir restriction in effect,解决php引入文件权限问题
查看>>
微信小程序获取用户信息解密AES并且注意如何获取unionid
查看>>
JavaScript设计模式----1
查看>>
Qt实现半透明遮罩效果
查看>>
erlang调优方法
查看>>
Mysql linux -N命令
查看>>
daily scrum 12.5
查看>>
linux-ftp install
查看>>
NetXray
查看>>
局域网基本工作原理
查看>>
让历史告诉我们未来
查看>>
UVa540 Team Queue
查看>>
android 练习之路 (八)
查看>>
tp5 中 model 的聚合查询
查看>>
android wear开发之:增加可穿戴设备功能到通知中 - Adding Wearable Features to Notifications...
查看>>
压缩文件函数库(转载)
查看>>
【转】ubuntu12.04没有/var/log/messages解决
查看>>
Oracle EBS 初始化用户密码
查看>>
SYS_CONTEXT 详细用法
查看>>
Pycharm配置autopep8让Python代码更符合pep8规范
查看>>