2012-08-06 105 views
1

我正在使用隨機訪問文件來使用arrayList存儲器寫入raf。我不知道它是否可以完成,但我正在嘗試,因爲它是創建此應用程序的最佳解決方案。使用隨機訪問文件的EOFException

這裏是我得到的運行時錯誤:

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", "[email protected]"); 




     //details.print(); 



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

     Student a = new Student("Becky O'Brien", "DKIT26", "0876126944", "[email protected]"); 
     Student b = new Student("Fabio Borini", "DKIT28", "0876136944", "[email protected]"); 
     Student c = new Student("Gaston Ramirez", "DKIT29", "0419834501", "[email protected]"); 
     Student d = new Student("Luis Suarez", "DKIT7", "0868989878", "[email protected]"); 
     Student e = new Student("Andy Carroll", "DKIT9", "0853456788", "[email protected]"); 
     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()); 

     } 



} 
+0

不'readChar'從文件讀取,不寫入? – 2012-08-06 12:44:51

+0

請不要在使用StringBuilder的時候使用StringBuffer(這是99%的時間) – 2012-08-06 12:46:23

回答

1

基本的問題是,你正在閱讀的數據比你寫的更多。

你正在閱讀30 char在你沒有寫的每條記錄的末尾。鑑於你放棄了它們,看起來你不需要這樣做。我會刪除studentTelephoneNumber

+0

我刪除了// temp = new char [30]; \t // for(int i = 0; i Pendo826 2012-08-06 12:51:19

+0

當我運行它沒有代碼時,它運行沒有錯誤。 – 2012-08-06 12:54:53

+0

這是如何有道理的。必須有一個替代問題。 – Pendo826 2012-08-06 12:56:04

2

您正在閱讀超越結束的文件。讀取不在那裏的數據。

0

後面的代碼如果只加載一個細節,您是否會遇到問題?我認爲你的問題是你正在閱讀文件的末尾。讀取15/30個字符的閱讀功能看起來很危險,因爲您可能沒有寫太多內容。爲什麼不讀,直到你得到一個逗號或其他分隔符? (如標籤)

這是這樣的事情,看起來危險

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

在什麼地方15從何而來?

嘗試並用一個細節分解並從那裏建立起來。你應該找到錯誤。我會親自閱讀,直到你找到一個特定的分隔符。

+0

你對一個細節正確。這顯示沒有錯誤。我會盡你所能表示感謝。 – Pendo826 2012-08-06 12:58:38

+0

繼續嘗試不同的細節,直到找到問題的細節。我懷疑這是名稱中少於15個字符的字符。這是使用幻數的問題。嘗試並使用分隔符。你可以讀取文件的整行內容,然後使用String.split()進行分割。這會給你一系列不同的部分 – RNJ 2012-08-06 13:02:29