2012-08-06 5 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'를 읽지 않고 쓰지 않습니까? –

+0

StringBuilder (99 %의 시간 사용 가능)를 사용할 수있는 경우 StringBuffer를 사용하지 마십시오. –

답변

1

기본적인 문제는 작성하는 것보다 더 많은 데이터를 읽는 것입니다.

당신은 쓰지 않은 각 레코드의 끝에 30 char을 읽고 있습니다. 당신이 그것을 버렸다고 가정하면, 당신은 이것을 할 필요가없는 것처럼 보입니다. studentTelephoneNumber

+0

// temp // new char [30]; \t // (int i = 0; i Pendo826

+0

코드없이 실행하면 오류없이 실행됩니다. –

+0

어떻게 이해가 가는지. 대체 문제가 있어야합니다. – Pendo826

2

파일의 거기에없는 데이터를 읽습니다.

0

로드 한 코드를 삭제하면 문제가 하나만 발생합니까? 나는 당신 문제가 당신이 파일의 끝을 읽고 있다는 것입니다. 15/30자를 읽는 읽기 기능은 그렇게 많이 쓰지 않았기 때문에 위험 해 보입니다. 쉼표 또는 다른 구분 기호를 얻을 때까지 읽지 않는 이유는 무엇입니까? (예 : 탭)

그것은 (15)는 어디에서 오는가

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

위험한 보이는 이런 종류의 물건이야?

거기에서 하나의 세부 사항 및 딱딱한 부분으로 시도해보십시오. 오류를 찾아야합니다. 나는 당신이 특정한 분리자를 찾을 때까지 개인적으로 읽을 것입니다.

+0

세부 사항 하나에 대해 정확합니다. 그것은 오류를 보여주지 않습니다. 너 한테 고마워하는 것을 할 것이다. – Pendo826

+0

세부 정보가 문제가 될 때까지 계속 다른 세부 정보를 시도하십시오. 나는 이것이 이름에 15 자 미만인 것으로 의심한다. 이것은 마술 번호를 사용하여 문제가됩니다. 분리 기호를 사용해보십시오. 파일의 전체 줄을 읽고 String.split()을 사용하여 분할 할 수 있습니다. 이것은 당신에게 줄의 다른 부분의 배열을 줄 것이다. – RNJ