使用随机访问文件的EOFException

时间:2012-08-06 12:40:14

标签: java file-io arraylist random-access eofexception

我正在使用随机访问文件使用arrayList存储编写raf。我不知道是否可以做到,但我试一试,因为这是我创建这个应用程序的最佳解决方案。

以下是我遇到的运行时错误:

Exception in thread "main" java.io.EOFException
at java.io.RandomAccessFile.readChar(Unknown Source)
at Student.read(Student.java:93)
at MainApp.start(MainApp.java:60)
at MainApp.main(MainApp.java:17)

这是我的代码:

MainApp
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.RandomAccessFile;


public class MainApp
{

    public static void main(String[] args) throws Exception 
    {
        new MainApp().start();

    }
    public void start()throws Exception 
    {
        StudentStore details = new StudentStore();
        //Student a = new Student("Becky O'Brien", "DKIT26", "0876126944", "bexo@hotmail.com");




        //details.print();



         RandomAccessFile raf = new RandomAccessFile("ContactDetails.txt", "rw");

         Student a = new Student("Becky O'Brien", "DKIT26", "0876126944", "bexo@hotmail.com");
         Student b = new Student("Fabio Borini", "DKIT28", "0876136944", "fabioborini@gmail.com");
         Student c = new Student("Gaston Ramirez", "DKIT29", "0419834501", "gramirez@webmail.com");
         Student d = new Student("Luis Suarez", "DKIT7", "0868989878", "luissuarez@yahoo.com");
         Student e = new Student("Andy Carroll", "DKIT9", "0853456788", "carroll123@hotmail.com");
         details.add(a);
         details.add(b);
         details.add(c);
         details.add(d);
         details.add(e);
            for (int i = 0; i < details.size(); i++) 
            {
              //a.setStudentName(a[i]);
              //a.setLastName(lnames[i]);
             // a.setAddress(addresses[i]);
             // a.setAge(ages[i]);
             // a.setSalary(salaries[i]);
              a.write(raf);
            }
            raf = new RandomAccessFile("employee.dat", "rw");

           // er = new Student();

            int numRecords = (int) raf.length() / details.size();

            for (int i = 0; i < numRecords; i++) {
              a.read(raf);

              System.out.print(a.getStudentName() + " ");
              System.out.print(a.getStudentId() + " ");
              System.out.print(a.getStudentEmail() + " ");
              System.out.print(a.getStudentTelephoneNumber() + " ");
            }
            raf.seek(0);
            for (int i = 0; i < numRecords; i++) 
            {
              a.read(raf);
                raf.seek(raf.getFilePointer() - details.size());
                a.write(raf);
                raf.seek(raf.getFilePointer() - details.size());
                a.read(raf);
              }
              System.out.print(a.getStudentName() + " ");
              System.out.print(a.getStudentId() + " ");
              System.out.print(a.getStudentEmail() + " ");
              System.out.print(a.getStudentTelephoneNumber() + " ");
            }

          }

学生

import java.io.IOException;
import java.io.RandomAccessFile;


public class Student 
{
//---------------------------------------------------------------------------
//  Class Variables.
//---------------------------------------------------------------------------   
    private String studentName;
    private String studentId;
    private String studentTelephoneNumber;
    private String studentEmail;
//---------------------------------------------------------------------------
//  Constructor.
//---------------------------------------------------------------------------   
    public Student(String studentName, String studentId,String studentTelephoneNumber, String studentEmail) 
    {
        this.studentName = studentName;
        this.studentId = studentId;
        this.studentTelephoneNumber = studentTelephoneNumber;
        this.studentEmail = studentEmail;
    }
//---------------------------------------------------------------------------
//  Getters.
//---------------------------------------------------------------------------
    public String getStudentName()
    {
        return studentName;
    }
    public String getStudentId() 
    {
        return studentId;
    }
    public String getStudentTelephoneNumber() 
    {
        return studentTelephoneNumber;
    }
    public String getStudentEmail() 
    {
        return studentEmail;
    }
//---------------------------------------------------------------------------
//  Setters.
//---------------------------------------------------------------------------
    public void setStudentName(String studentName) 
    {
        this.studentName = studentName;
    }
    public void setStudentId(String studentId) 
    {
        this.studentId = studentId;
    }
    public void setStudentTelephoneNumber(String studentTelephoneNumber) 
    {
        this.studentTelephoneNumber = studentTelephoneNumber;
    }
    public void setStudentEmail(String studentEmail) 
    {
        this.studentEmail = studentEmail;
    }
//---------------------------------------------------------------------------
//  toString.
//---------------------------------------------------------------------------
    public String toString() 
    {
        return "---------------------------Student--------------------------- " +
                "\nStudent Name:" + studentName + 
                "\nStudent Id:"+ studentId + 
                "\nStudent Telephone Number:"+ studentTelephoneNumber + 
                "\nStudent Email:" + studentEmail;
    }
    void read(RandomAccessFile raf) throws IOException 
    {
        char[] temp = new char[15];
        for (int i = 0; i < temp.length; i++)
          temp[i] = raf.readChar();
        studentName = new String(temp);
        temp = new char[15];
        for (int i = 0; i < temp.length; i++)
          temp[i] = raf.readChar();
        studentId = new String(temp);
        temp = new char[30];
        for (int i = 0; i < temp.length; i++)
          temp[i] = raf.readChar();
        studentEmail = new String(temp);
        temp = new char[30];
        for (int i = 0; i < temp.length; i++)
              temp[i] = raf.readChar();
        studentTelephoneNumber = new String(temp);
        temp = new char[30];
        for (int i = 0; i < temp.length; i++)
              temp[i] = raf.readChar();
      }

      void write(RandomAccessFile raf) throws IOException 
      {
        StringBuffer sb;
        if (studentName != null)
          sb = new StringBuffer(studentName);
        else
          sb = new StringBuffer();

        sb.setLength(15);
        raf.writeChars(sb.toString());

        if (studentId != null)
          sb = new StringBuffer(studentId);
        else
          sb = new StringBuffer();

        sb.setLength(15);
        raf.writeChars(sb.toString());

        if (studentEmail != null)
          sb = new StringBuffer(studentEmail);
        else
          sb = new StringBuffer();

        sb.setLength(30);
        raf.writeChars(sb.toString());
        if (studentTelephoneNumber != null)
              sb = new StringBuffer(studentTelephoneNumber);
            else
              sb = new StringBuffer();

            sb.setLength(30);
            raf.writeChars(sb.toString());

      }



}

3 个答案:

答案 0 :(得分:2)

您正在阅读超出文件末尾的内容。读取不存在的数据。

答案 1 :(得分:1)

基本问题是你正在阅读的数据比你写的更多。

您正在阅读每条记录末尾30 char,但您没有写。鉴于你丢弃它们,你似乎不需要这样做。我会删除在studentTelephoneNumber

之后读取的代码

答案 2 :(得分:0)

如果加载一个细节,你会遇到问题吗?我认为你的问题是你正在阅读文件的末尾。读取15/30个字符的读取功能看起来很危险,因为您可能写得不多。为什么不读,直到你得到一个逗号或其他分隔符? (例如标签)

这种看起来很危险的事情

char[] temp = new char[15]; 
for (int i = 0; i < temp.length; i++) 
    temp[i] = raf.readChar(); 

15来自哪里?

尝试用一个细节打破它,并从那里加强。你应该找到错误。我会亲自阅读,直到找到一个特定的分隔符。