关于RandomAccessFile类

时间:2013.09.04 发布人:yn_xinhua

关于RandomAccessFile类

已解决问题

谷歌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();
}
}期待您的答案,谢谢你帮我,祝你快乐,愿你事事如意 !

希望以下的回答,能够帮助你。

第1个回答

用户名:zzlovenini  

首先你的预通般输出文件:student.印富价矛获轴架txt命名有问题,在windows平台下,这个文件的长度,不是你写入**的具体长度,平台可能会加上几个字符(你可以把第一个stu草念密乙问促宣dent对象的名字改神增皮按至移弦紧成"a"实验一下),你的测试没有抛异常,可能是你的测试代码正好能组成一个.t季死显易朝握酒白频门联xt格式的文件,审况径额所以平台没有给你加字节。我觉得最好还是不要写文件的类型名,因为在不同平台下,文件的实现别单识投促导专量是不同的,Windows下是NTFS,FAT32,Li**x下是exts3,对于.txt的文件的解释可能是不同的

要的错误是程序的逻辑错误,Students=newStudent();这句你new了一个新的学生,至确管宗宪并卫这个学生对象的n间另差um=0,name=**ll,score=0.0限烈,你从student.txt文件中读出来的**并没有赋值给s的各个分量,所以输出结果是:编号圆总周环蛋此故且氧鱼位0姓名**ll得分0.0

下面是我的测试代码:

-------------冲--------------------------------------------罪伤书属斯额敌然建事----------

for(longi=0;i<raf.length();i=raf.getFilePointer()){
//s.readStudent(raf);
//阳若并如打System.out.println("编号"+s.**m+"姓名"+s.name+"得分"+s.score);
System.out.println(raf.readIn空求汽由故训举t()+""+raf.readUTF()+""+raf.readDoubl远含考川跟e()+"\n");
}

------------------------------------------------------------

如果就这个问题来说,我觉得用ObjectInputStream和ObjectOutputStream比较好理解一些

逻辑错误就是我上面说的,你生成一个student对象时,属性都是空值,你取得到的肯定都是空值了,关读死云父稳江剧货响诉这就是你的逻辑错误,你的想法是把从文件中读到的值赋值给对象,但是没有赋值。