0:null:0.0
0:null:0.0
0:null:0.0
为什么结果都是这样的呢?

解决方案 »

  1.   

    import java.io.RandomAccessFile; import java.io.*; public class RandomFileTest { 
        public static void main(String[] args) throws Exception { 
    Student s1 = new Student(1, "zhangsan", 98.5); 
    Student s2 = new Student(2, "lisi", 94); 
    Student s3 = new Student(3, "wangwu", 58); 
    RandomAccessFile raf = new RandomAccessFile("student.txt", "rw"); 
    s1.writeStudent(raf); 
    s2.writeStudent(raf); 
    s3.writeStudent(raf); 
    Student s = new Student(); 
    raf.seek(0); 
    for (long i = 0; i < raf.length(); i = raf.getFilePointer()) { 
         s.readStudent(raf); 
         System.out.println(s.num + ":" + s.name + ":" + s.score); 

    raf.close();  } 
    } class Student { 
        int num; 
        String name; 
        double score;     Student() { 
        }     Student(int n, String na, double sc)     { 
    num = n; 
    name = na; 
    score = sc; 
        }     public void writeStudent(RandomAccessFile raf) throws IOException  { 
    raf.writeInt(num); 
    raf.writeUTF(name); 
    raf.writeDouble(score);     }     public void readStudent(RandomAccessFile raf) throws IOException     { 
    num = raf.readInt();   // 这里
    name = raf.readUTF();   //这里
    score = raf.readDouble();  //还有这里    } 
      

  2.   

    你读了没有赋值this.num = raf.readInt(); 
    this.name = raf.readUTF(); 
    this.score = raf.readDouble(); 
      

  3.   

    建议楼主以后遇到类似空值问题
    在程序中设断点然后debug 程序
    然后一步一步调