2012-12-20 187 views
-3

嗨,大家好,我只是實現目標文件到我的程序和我不斷得到錯誤(錯誤閱讀文件和問題寫入文件),這些都是在我的try catch塊2級的錯誤,當我嘗試讀取它不加載的文件,保存也不起作用。錯誤讀取和寫入文件(JAVA)

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import java.util.*; 


public class Stores implements Serializable 
{ 
    public static ArrayList<Student> stud1 = new ArrayList<Student>(); 
    public static ArrayList<SubjectTeacher> sTeach1 = new ArrayList<SubjectTeacher>(); 
    private static int iT = 0; 
    private static int iS = 0; 


    public static void savet (ArrayList<SubjectTeacher> teachIn, int count) 
     { 
      iT = count; 
      sTeach1 = teachIn; 
      saveTeachToFile(); 
     } 

    public static void saves (ArrayList<Student> studIn, int count) 
     { 
      iS = count; 
      stud1 = studIn; 
      saveStudToFile(); 
     } 

    public static ArrayList<Student> getStud() 
     { 
      return stud1; 
     } 

    public static ArrayList<SubjectTeacher> getTeach() 
     { 
      return sTeach1; 
     } 

    public static int getStudSize() 
     { 
      return stud1.size(); 
     } 

    public static int getTeachSize() 
     { 
      return sTeach1.size(); 
     } 

    private static void saveStudToFile() 
    { 
     try 
     { 
      // create a FileOutputStream object which will handles the writing of the sudent list of objects to the file. 
      FileOutputStream studentFile = new FileOutputStream("Students.obf"); 
      // the OutputObjectStream object will allow us to write whole objects to and from files 
      ObjectOutputStream studentStream = new ObjectOutputStream(studentFile); 

      for(Student item: stud1) // enhanced for loop 
      // Loop through the list of studentsListIn and for each of these objects, wite them to the file 
      { 
       studentStream.writeObject(item); 
      } 
      //close the file so that it is no longer accessible to the program 
      studentStream.close(); 
     } 

     catch(IOException e) 
     { 
      System.out.println("There was a problem writing the File"); 
     } 
    } 

    private static void saveTeachToFile() 
    { 
     try 
     { 
      FileOutputStream teacherFile = new FileOutputStream("Teacher.obf"); 
      ObjectOutputStream teacherStream = new ObjectOutputStream(teacherFile); 

      for(SubjectTeacher item1: sTeach1) // enhanced for loop 
      { 
       teacherStream.writeObject(item1); 
      } 
      //close the file so that it is no longer accessible to the program 
      teacherStream.close(); 
     } 

     catch(IOException e) 
     { 
      System.out.println("There was a problem writing the File"); 
     } 
    } 

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

     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 
      tempStudent = (Student) studentStream.readObject(); 

      while (endOfFile != true) 
      { 
       try 
       { 
        stud1.add(tempStudent); 
        // read the next (whole) object 
        tempStudent = (Student) studentStream.readObject(); 
       } 

       //use the fact that the readObject throws an EOFException to check whether the end of eth file has been reached 
       catch(EOFException e) 
       { 
        endOfFile = true; 
       } 

       studentStream.close(); 
      } 
     } 
     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"); 
     } 
    } 

    public static void loadTeacherList() 
    { 
     boolean endOfFile = false; 
     SubjectTeacher tempTeacher; 

     try 
     { 

      FileInputStream teacherFile = new FileInputStream("Teacher.obf"); 

      ObjectInputStream teacherStream = new ObjectInputStream(teacherFile) ; 


      tempTeacher = (SubjectTeacher) teacherStream.readObject(); 

      while (endOfFile != true) 
      { 
       try 
       { 
        sTeach1.add(tempTeacher); 
        // read the next (whole) object 
        tempTeacher = (SubjectTeacher) teacherStream.readObject(); 
       } 

       //use the fact that the readObject throws an EOFException to check whether the end of eth file has been reached 
       catch(EOFException e) 
       { 
        endOfFile = true; 
       } 

       teacherStream.close(); 
      } 
     } 
     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"); 
     } 
    } 


} 
+4

我沒有看到一個try catch塊 –

+1

...或文件的讀/寫 –

+0

什麼錯誤?他們在哪裏發生?你期望發生什麼? – unholysampler

回答

1

嗯,一方面,您應該使用正確的代碼編輯問題,以免它關閉。其次,可能會發生一些事情。

  1. 你寫文件的類不能序列
  2. 這些文件只讀或寫保護莫名其妙

根據您的更新問題的代碼,它看起來像你可能混淆哪些類需要實現Serializable。需要實現的類是您實際寫入文件的類(即SubjectTeacher等)。

檢查這兩個,讓我知道你發現了什麼。

另外,我建議步進代碼,看到異常的樣子在運行時。你會更清楚發生了什麼。

+0

謝謝,再次遺憾的誤會,遺憾的是沒有工作 –

+0

檢測出來它的工作原理:d,但它仍然顯示有關讀取文件中的錯誤(儘管它實際上是紅色的) –

+0

@MatthewCassar什麼異常說?通過它的屬性來查看描述(我忘記了在java中實際調用的屬性 - 我一直在編寫c#代碼一段時間) –