2013-01-24 3 views
-1

나는 그것이 파일을로드에 관해서 그러나 나는 (저장 모두 OK입니다)Java에서 파일 오류로드 중?

public static void loadStudentList() { 
    boolean endOfFile = false; 

    try { 
     // create a FileInputStream object, studentFile 
     FileInputStream studentFile = new FileInputStream("Students.obf"); 
     // create am ObjectImnputStream object to wrap around studentStream 
     ObjectInputStream studentStream = new ObjectInputStream(studentFile) ; 

     // read the first (whole) object with the readObject method 
     Student tempStudent = (Student) studentStream.readObject(); 
     while (endOfFile != true) { 
      try { 
       tempStudent = (Student) studentStream.readObject(); 
       stud1.add(tempStudent); 
      } 
      catch(EOFException e) { 
       endOfFile = true; 
      } 
     } 
     studentStream.close(); 
     //use the fact that the readObject throws an EOFException to check whether the end of eth file has been reached 
    } 
    catch(FileNotFoundException e) { 
     System.out.println("File not found"); 
    } 

    catch(ClassNotFoundException e) { // thrown by readObject 
    /* which indicates that the object just read does not correspond to any class 
known to the program */ 
     System.out.println("Trying to read an object of an unkonown class"); 
    } 
    catch(StreamCorruptedException e) { //thrown by constructor 
    // which indicates that the input stream given to it was not produced by an ObjectOutputStream object 
     System.out.println("Unreadable File Format"); 
    } 
    catch(IOException e) { 
     System.out.println("There was a problem reading the file"); 
    } 
} 

이 내가 파일을로드하는 데 사용되는 코드 문제가 발생하고, 자바 프로젝트에 대한 오브젝트 파일을 구현해야합니다. 프로그램은 내 파일의 마지막 두 레코드 인 을로드합니다. 아이디어는 프로그램에서 나중에 사용할 수 있도록 모든 프로그램을 배열 목록에로드하는 것입니다. 또한 나는 어떤 포로도 돌려받지 못한다. 어떤 도움이 필요합니까? 감사합니다 :)

+0

처음으로 검색되는 Student 인스턴스는 저장되지 않습니다. stud1 컬렉션. 파일에 3 개의 레코드 만 포함되어있을 수 있습니까? – Henrik

+0

몇 개의 개체를로드 할 예정입니까? 파일의 내용을 게시하십시오. – TechSpellBound

답변

0

당신은 결코 나는이 확실하지 않다

 while (endOfFile != true) 
    { 
     try 
     { 

      Student tempStudent = (Student) studentStream.readObject(); 
      stud1.add(tempStudent); 
     } 

아래의 코드처럼 잠시 전에 읽기를 제거 목록에 당신이

Student tempStudent = (Student) studentStream.readObject(); 
     while (endOfFile != true) 
     { 
      try 
      { 

       tempStudent = (Student) studentStream.readObject(); 
       stud1.add(tempStudent); 
      } 

을 읽을 최초의 학생을 추가하지 귀하의 문제를 해결할 것입니다

0

왜 당신이 다음 을 파일에 직렬화/쓰 ArrayList<Type>에 개체를 추가하지 않고 다음 읽기/직렬화, 하나 ArrayList<Type>로 데이터를 읽습니다.

그럼 당신은 그 일의 쉬운 문제가없는 방법이 될 수있는 ArrayList를

이에서 하나의 오브젝트를 가져올 수 있습니다.

//Serialize 
    ArrayList<Student> students = new ArrayList<Student>(); 
    //Add the student objects to the array list 
    File f = new File("FileName.ser"); 
    ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream(f)); 
    objOut.writeObject(students); 

    //Deserialize 
    ArrayList<Student> students = new ArrayList<Student>(); 
    ObjectInputStream objIn = new ObjectInputStream(new FileInputStream(new File("FileName.ser"))); 
    students = (ArrayList<String>) objIn.readObject(); 
+0

파일의 각 개체에 .add 메서드를 사용하여 작업하고 있습니다. –

+0

제 편집 내용을 확인하십시오. –