老是出现异常,在str.reader(raf)这行,求解释

Peng4Wang1 2013-08-06 10:39:23
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package NIOTest;

import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Iterator;

/**
*
* @author Administrator
*/
public class RandomaccessTest {
public static void main(String[] args) throws Exception {
RandomAccessFile raf = new RandomAccessFile("e:/abc/2.txt","rw");
ArrayList<Student>list = new ArrayList<Student> ();
list.add(new Student(101,"xiaoyi",58.5));
list.add(new Student(102,"xiaoer",98));
Student st= null;
for(Iterator iter = list.iterator();iter.hasNext();)
{
st = (Student)iter.next();
st.Writer(raf);
}
Student str =new Student();
raf.seek(0);

for(Iterator iter = list.iterator();iter.hasNext();)
{
str = (Student)iter.next();
str.reader(raf);
System.out.println(str);
}
raf.close();


}

}
class Student
{
int id ;
String name;
double score;

public Student() {
}

public Student(int id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getScore() {
return score;
}

public void setScore(double score) {
this.score = score;
}
public void Writer(RandomAccessFile raf) throws Exception
{
raf.write(id);
raf.writeUTF(name);
raf.writeDouble(score);

}
public void reader(RandomAccessFile raf) throws Exception
{
id = raf.readInt();
name = raf.readUTF();
score = raf.readDouble();
}

@Override
public String toString() {
return "Student{" + "id=" + id + ", name=" + name + ", score=" + score + '}';
}



}
...全文
117 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
fedori 2013-08-08
  • 打赏
  • 举报
回复
raf.write⇒raf.writeInt

public void Writer(RandomAccessFile raf) throws Exception {
		raf.writeInt(id);
		raf.writeUTF(name);
		raf.writeDouble(score);

	}
valid26 2013-08-08
  • 打赏
  • 举报
回复
raf.writeInt(id);
代号裤子 2013-08-08
  • 打赏
  • 举报
回复
楼上正解 raf.writeInt(id);

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧