老是出现异常,在str.reader(raf)这行,求解释
/*
* 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 + '}';
}
}