2014-01-25 28 views
0

我有我的方案的能力,以我的地圖保存到一個文件的麻煩。以下是我寫作和閱讀我的地圖和陣列列表的兩種方法。遇到問題,ObjectInputStream的/ OutputStream的

這裏是我的讀法:

private void getData() throws IOException, ClassNotFoundException { 
    File f_Instructors = new File(PSLTrackerInfo.file + "instructors.brent"); 
    File f_Students = new File(PSLTrackerInfo.file + "students.brent"); 
    File f_Times = new File(PSLTrackerInfo.file + "times.brent"); 
    if (f_Instructors.exists()) { 
     try (ObjectInputStream in = new ObjectInputStream(new 
       BufferedInputStream(new FileInputStream(f_Instructors)))) { 
      //Add theList back in 
      if (in.readObject() != null) { 
       TreeMap<Instructor, Set<Student>> read = null; 
       while(in.available() > 0) { 
        read = (TreeMap<Instructor, Set<Student>>) 
          in.readObject(); 
       } 
       if (read != null) { 
        for (Instructor key : read.keySet()) { 
         System.out.println(key); 
         Set<Student> values = read.get(key); 
         PSLTrackerInfo.addInstructor(key, values); 
        } 
        System.out.println("Instructors Found! Reading..."); 
       } else { 
        System.out.println("No instructor data saved.1"); 
       } 
      } else { 
       System.out.println("No instructor data saved.2"); 
      } 
      in.close(); 
     } 
    } 
    //Add times back in 
    if (f_Times.exists()) { 
     try (ObjectInputStream in = new ObjectInputStream(new 
       BufferedInputStream(new FileInputStream(f_Times)))) { 
      if (in.readObject() != null) { 
       TreeMap<Student, ArrayList<Date>> readTimes = null; 
       while(in.available() > 0) { 
        readTimes = (TreeMap<Student, ArrayList<Date>>) in.readObject(); 
       } 
       if (readTimes != null) { 
        for (Student key : readTimes.keySet()) { 
         System.out.println(key); 
         ArrayList<Date> values = readTimes.get(key); 
         PSLTrackerInfo.addTimes(key, values); 
        } 
        System.out.println("Dates Found! Reading..."); 
       } else { 
        System.out.println("No dates saved."); 
       } 
      } else { 
       System.out.println("No dates saved."); 
      } 
      in.close(); 
     } 
    } 
    //Add newStudents back in 
    if (f_Students.exists()) { 
     try (ObjectInputStream in = new ObjectInputStream(new 
       BufferedInputStream(new FileInputStream(f_Students)))) { 
      if (in.readObject() != null) { 
       ArrayList<Student> readStudents = null; 
       while (in.available() > 0) { 
        readStudents = (ArrayList<Student>) in.readObject(); 

       } 
       if (readStudents != null) { 
        PSLTrackerInfo.setTheList(readStudents); 
       } 
       System.out.println("New students found! Reading..."); 
      } else { 
       System.out.println("No new students data saved."); 
      } 
      in.close(); 
     } 
    } 
} 

,這裏是我的寫作方法:

private void saveData() { 
    System.out.println("Saving Data..."); 
    File f_Instructors = new File(PSLTrackerInfo.file + "instructors.brent"); 
    File f_Students = new File(PSLTrackerInfo.file + "students.brent"); 
    File f_Times = new File(PSLTrackerInfo.file + "times.brent"); 
    ObjectOutputStream out_Instructors = null; 
    ObjectOutputStream out_Students = null; 
    ObjectOutputStream out_Times = null; 
    try { 
     out_Instructors = new ObjectOutputStream(new 
       BufferedOutputStream(new FileOutputStream(f_Instructors))); 
     out_Students = new ObjectOutputStream(new 
       BufferedOutputStream(new FileOutputStream(f_Students))); 
     out_Times = new ObjectOutputStream(new 
       BufferedOutputStream(new FileOutputStream(f_Times))); 
     out_Instructors.writeObject(PSLTrackerInfo.getMap()); 
     out_Times.writeObject(PSLTrackerInfo.getTimes()); 
     out_Students.writeObject(PSLTrackerInfo.getList()); 
     out_Instructors.flush(); 
     out_Students.flush(); 
     out_Times.flush(); 
     out_Instructors.close(); 
     out_Students.close(); 
     out_Times.close(); 
    } catch (IOException ex) { 
     Logger.getLogger(PrivateLessonsTrackerGUI.class.getName()) 
       .log(Level.SEVERE, null, ex); 
    } 
    System.exit(0); 
} 

很抱歉,如果它是一個有點混亂,我有3個文件,以節省3名不同的對象,如果有是一種將其保存到一個文件中,讓我知道,但我只是得到了很多的錯誤,我無法弄清楚如何解決所以這是我落得這樣做。感謝您給予的任何幫助。

要EJP:我想這

TreeMap<Instructor, Set<Student>> read = null; 
try { 
    read = (TreeMap<Instructor, Set<Student>>) 
      in.readObject(); 
} catch (EOFException e) { 
    System.out.println("Caught EOFException!"); 
} 

而且當它被寫入文件,即使有在它的數據,我得到了一個EOFException類每次。

回答

0
  1. in.readObject()不會返回null,除非您寫入null。如果你使用它作爲流結束的測試,它是無效的。正確的技術是捕獲EOFException。

  2. 要調用它,扔掉的結果,如果它不爲空,然後再調用它。第二個電話,如果沒有在文件中另一個對象拋EOFException類。它不會給你第一個電話的結果。這是一個流。

  3. in.available()也不是流結束的有效測試。這不是它的目的。看到Javadoc。再次,用的readObject()正確的技術是捕捉EOFException類。

+0

我添加了我的try/catch塊,但是每次運行時,即使在地圖中的數據寫入文件時,我也會收到異常。 – Brent

+0

我給你約六件事情來解決,而不是僅僅try/catch塊。你需要解決它們*全部*編輯中的新代碼沒有顯示任何符號。 – EJP