JAVA RandomAccessFile 问题
来源:百度知道 编辑:UC知道 时间:2024/06/30 08:20:30
class RAF {
public static void main(String[] args) throws Exception {
Student s1 = new Student(1, "zhangsan", 98.5);
Student s2 = new Student(2, "lisi", 96.5);
Student s3 = new Student(3, "wangwu", 78.5);
RandomAccessFile raf = new RandomAccessFile("student.txt", "rw");
s1.writeStudent(raf);
s2.writeStudent(raf);
s3.writeStudent(raf);
// 将文件指针设为起始位置
raf.seek(0);
for (long i = 0; i < raf.length(); i=raf.getFilePointer()) {
System.out.println(raf.readInt()+" "+raf.readUTF()+" "+raf.readDouble()+"\n");
}
raf.close();
}
}
class Student {
int num;
String name;
double score;
public Student(int num, String name, double score) {
this.num = num;
this.name = name;
this.score = score;
}<
你的主类应该声明为public类型。
import java.io.*;
class RAF {
public static void main(String[] args) throws Exception {
Student s1 = new Student(1, "zhangsan", 98.5);
Student s2 = new Student(2, "lisi", 96.5);
Student s3 = new Student(3, "wangwu", 78.5);
RandomAccessFile raf = new RandomAccessFile("student.txt", "rw");
s1.writeStudent(raf);
s2.writeStudent(raf);
s3.writeStudent(raf);
// 将文件指针设为起始位置
raf.seek(0);
for (long i = 0; i < raf.length(); i=raf.getFilePointer()) {
System.out.println(raf.readInt()+" "+raf.readUTF()+" "+raf.readDouble()+"\n");
}
raf.close();
}
}
class Student {
int num;
String name;
double score;
public Student(int num, String name, double score) {
this.num = num;
this.name = name;
t