当前位置:编程学习 > JAVA >>

老是出现异常,在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 + '}';
    }
    

    
}
--------------------编程问答-------------------- raf.write⇒raf.writeInt

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

}
--------------------编程问答-------------------- 楼上正解   raf.writeInt(id); --------------------编程问答-------------------- raf.writeInt(id); 
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,