已解决问题
谷歌yn_xinhua用户在2013.09.04提交了关于“腾讯体育关于RandomAccessFile类”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2024-08-27T07:10:56。我按照教程上说的,使用RandomAccessFile类,代码都是一样的,为什么我的运行结果却有问题呢,哪位高手帮忙看下,谢谢了importj**a.io.IOException;
importj**a.io.RandomAccessFile;
publicclassRandomFileTest{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args)throwsException{
Students1=newStudent(1,"zhangsan",98.5);
Students2=newStudent(2,"lisi",96.5);
Students3=newStudent(3,"wangwu",78.5);
RandomAccessFileraf=newRandomAccessFile("student.txt","rw");
s1.writeStudent(raf);
s2.writeStudent(raf);
s3.writeStudent(raf);
Students=newStudent();
//将文件指针设为起始位置
***.seek(0);
for(longi=0;i<raf.length();i=raf.getFilePointer()){
s.readStudent(raf);
System.out.println("编号"+s.**m+"姓名"+s.name+"得分"+s.score);
}
raf.close();
}
}
classStudent{
int**m;
Stringname;
doublescore;
publicStudent(int**m,Stringname,doublescore){
this.**m=**m;
***.name=name;
this.score=score;
}
publicStudent(){
}
publicvoidwriteStudent(RandomAccessFileraf)throwsIOException{
raf.writeInt(**m);
raf.writeUTF(name);
raf.writeDouble(score);
}
publicvoidreadStudent(RandomAccessFileraf)throwsIOException{
raf.readInt();
raf.readUTF();
raf.readDouble();
}
}希望大家能够帮助她。
详细问题描述及疑问:我按照教程上说的,使用RandomAccessFile类,代码都是一样的,为什么我的运行结果却有问题呢,哪位高手帮忙看下,谢谢了
importj**a.io.IOException;
importj**a.io.RandomAccessFile;
publicclassRandomFileTest{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args)throwsException{
Students1=newStudent(1,"zhangsan",98.5);
Students2=newStudent(2,"lisi",96.5);
Students3=newStudent(3,"wangwu",78.5);
RandomAccessFileraf=newRandomAccessFile("student.txt","rw");
s1.writeStudent(raf);
s2.writeStudent(raf);
s3.writeStudent(raf);
Students=newStudent();
//将文件指针设为起始位置
***.seek(0);
for(longi=0;i<raf.length();i=raf.getFilePointer()){
s.readStudent(raf);
System.out.println("编号"+s.**m+"姓名"+s.name+"得分"+s.score);
}
raf.close();
}
}
classStudent{
int**m;
Stringname;
doublescore;
publicStudent(int**m,Stringname,doublescore){
this.**m=**m;
***.name=name;
this.score=score;
}
publicStudent(){
}
publicvoidwriteStudent(RandomAccessFileraf)throwsIOException{
raf.writeInt(**m);
raf.writeUTF(name);
raf.writeDouble(score);
}
publicvoidreadStudent(RandomAccessFileraf)throwsIOException{
raf.readInt();
raf.readUTF();
raf.readDouble();
}
}期待您的答案,谢谢你帮我,祝你快乐,愿你事事如意
!