高手帮忙看下这段代码总输出0:null:0.0
/**
*
*/
package test;
import
java.io.FileNotFoundException;
import
java.io.IOException;
import java.io.RandomAccessFile;
/**
* @author Administrator
*
*/
public class TestRandomAccessFile {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s1=new Student(1,"linda",100);
Student s2=new Student(2,"king",99);
Student s3=new Student(3,"shanqiu",98);
try {
RandomAccessFile rda=new RandomAccessFile("F://test/test.txt","rw");
s1.writeStudent(rda);
s2.writeStudent(rda);
s3.writeStudent(rda);
Student s=new Student();
rda.seek(0);
for(long i=0;i<rda.length();i=rda.getFilePointer()){
s.readStudent(rda);
System.out.println(s.num+":\t"+s.name+":\t"+s.score);
}
rda.close();
} catch (
FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Student{
int num;
String name;
double score;
public Student(){}
public Student(int num,String name,double score){
this.num=num;
this.name=name;
this.score=score;
}
public void writeStudent(RandomAccessFile raf)throws IOException{
raf.writeInt(num);
raf.writeUTF(name);
raf.writeDouble(score);
}
public void readStudent(RandomAccessFile raf)throws IOException{
raf.readInt();
raf.readUTF();
raf.readDouble();
}
}
------解决方案--------------------this.num = raf.readInt();
this.name = raf.readUTF();
this.score = raf.readDouble();
------解决方案--------------------s.readStudent(rda);
System.out.println(s.num+":\t"+s.name+":\t"+s.score);
用的是无参构造函数,并且没有为成员变量赋值,所以为默认值0,null,0.0