package com.company.project;
import java.io.IOException;
import java.io.RandomAccessFile;
import com.company.project.Student;;
public class RandomAccessFileTest {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("D://cola//jing.txt","rw");
Student student1 = new Student();
Student student2 = new Student();
student1.setId(11);
student1.setName("jia");
student1.setScore(99f);
student2.setId(11);
student2.setName("jing");
student2.setScore(99f);
writeFile(raf, student1);
writeFile(raf, student2);
System.out.println(readFile(raf, 0));
System.out.println(readFile(raf, raf.getFilePointer())); //getFilePointer() 返回当前指针的偏移位置 返回类型long
raf.close();
}
public static void writeFile(RandomAccessFile raf, Student student) throws IOException{
raf.writeInt(student.getId());
raf.writeUTF(student.getName());
raf.writeFloat(student.getScore());
}
public static Student readFile(RandomAccessFile raf,long pos) throws IOException{
Student student = new Student();
raf.seek(pos);
student.setId(raf.readInt());
student.setName(raf.readUTF());
student.setScore(raf.readFloat());
return student;
}
}
RandomAccessFile
原创文章标签 RandomAccessFile 文章分类 Java 后端开发
©著作权归作者所有:来自51CTO博客作者colapanda的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:eclipse 快捷键
下一篇:BufferedReader
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章