2014-02-18 86 views
0

我創建了一個對象,然後將其添加到ArrayList中。然後我使用ObjectOutputStream將這個數組列表保存到一個.dat文件中。我知道該對象已被正確創建,因爲程序中的下一個屏幕直接從對象中加載,之後可以從文件中正確地讀取對象。我遇到的問題是,似乎該對象正在保存,但不願意被編輯。不是編輯數組列表中的對象,而是每次創建一個新對象並再次保存它。Java保存功能不會覆蓋

下面的代碼顯示每次有人完成最後一個屏幕時執行的保存功能。我正在努力做到這一點,以便檢查學生是否已經存在於數組中,如果只是編輯該對象。如果學生不存在,我希望它將selectedStudent(用於所有GUI函數的對象)添加到數組「學生」中,並將該數組寫入文件,覆蓋以前的所有數據。

@SuppressWarnings("unchecked") 
public static void saveNew() throws ClassNotFoundException, IOException{ 
    int exists = -1; 
    try{ 
     FileInputStream fis = new FileInputStream("records.dat"); 
     ObjectInputStream in = new ObjectInputStream(fis); 

     students = (ArrayList<Student>) in.readObject(); 
     in.close(); 
     fis.close(); 
    } 
    catch(IOException e){ 
     e.printStackTrace(); 
    } 
    try{ 
     FileOutputStream fos = new FileOutputStream("records.dat", false); 
     ObjectOutputStream out = new ObjectOutputStream(fos); 

     for(int i = 0; i<students.size(); i++){ 
      if(students.get(i).getID().equals(selectedStudent.getID())){ 
       exists = i; 
      } 
     } 
     if(exists<0){ 
      students.add(selectedStudent); 
     } 
     else{ 
      students.set(i, selectedStudent); 
     } 


     out.writeObject(students); 
     out.close(); 
    } 
    catch(IOException e){ 
     e.printStackTrace(); 
    } 
} 

編輯:我注意到,該變量存在沒有被用於搜索這是錯誤的頭號目標,但我仍然有其中保存的對象將不會被改變,直到該方法被稱爲問題第二次。當它再次運行時似乎可以找到它,但是當它第一次運行時,它只會創建一個帶有編輯名稱的新學生。

例如,第一個學生被創建並保存。第二個學生是創建和保存的。當正在編輯第二名學生時(不關閉程序並重新啓動),它將不會編輯文件中的對象,而是直接在第一個下面創建一個具有相同信息的新學生對象。如果再次運行編輯功能,則會正確編輯第二個學生文件,但會保持第一個原樣。

+0

此代碼不能編譯。 – Leo

+0

這是來自整個代碼的示例片段。代碼包含了很多東西,如果沒有我發佈所有涉及的類,就不會運行。就目前來看,其他一切都在運行並且工作得很好,我只是在創建保存方法時遇到了問題。我擁有它,所以每次在最後一個(和非可選)菜單上點擊保存按鈕時,都會調用保存方法進行編輯和創建。因此,每次在將信息更改爲對象selectedStudent結束時都會運行此代碼。 – KM529

回答

0

因爲一開始我會編輯這些行

if(students.get(i).getID().equals(selectedStudent.getID())){ 
    exists = i; 
} 

if(students.get(i).getID().equals(selectedStudent.getID())){ 
    System.out.println ("Got it"); 
    exists = i; 
    break; 
} 

公正,以確保其工作正常。

而且,你要使用的i更改爲exists

else{ 
    students.set(i, selectedStudent); // change to exists 
} 
+0

我注意到存在的變量沒有被用來搜索第一個錯誤的對象,但是我仍然有問題,直到第二次調用該方法時,保存的對象纔會被更改。當它再次運行時似乎可以找到它,但是當它第一次運行時,它只會創建一個帶有編輯名稱的新學生。 – KM529

+0

我已經更新了我的答案 –

0

我認爲你必須檢查你的變量(如果你打算重用的話)和初始化代碼。

你發佈的代碼片段似乎很好,所以我找不到它的錯誤。

這裏有一個相當類似的代碼。我希望它有幫助。

import java.io.EOFException; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.util.ArrayList; 

public class Persistence { 

    public static void main(String[] args) throws Exception { 
     File f = new File("records.dat"); 
//  f.delete(); 
     if (!f.exists()){ 
      f.createNewFile(); 
     } 

     Persistence p = new Persistence(); 
     if (p.peek() == null){ 
      p.init(); //persist an empty list 
     } 

     p.saveNew(new Student("ID1","Some Value")); //must insert 
     p.saveNew(new Student("ID1","Some Other Value")); //must edit 
     p.saveNew(new Student("ID2","Some Value")); //must insert 

     ArrayList<Student> list = p.peek(); 
     System.out.println(list); 
    } 

    private void save(ArrayList<Student> list) throws Exception{ 
     FileOutputStream fos = new FileOutputStream("records.dat",false);//don't append 
     ObjectOutputStream out = new ObjectOutputStream(fos); 
     out.writeObject(list); 
     out.flush(); 
     out.close(); 
     fos.close(); 
    } 

    private void init() throws Exception{ 
     save(new ArrayList<Student>()); 
    } 

    private ArrayList<Student> peek() throws Exception{ 
     FileInputStream fis = new FileInputStream("records.dat"); 
     try{ 
      ObjectInputStream in = new ObjectInputStream(fis); 
      ArrayList<Student> students = (ArrayList<Student>) in.readObject(); 
      return students; 
     }catch(EOFException eof){ 
      return null; 
     }finally{ 
      fis.close(); 
     } 
    } 

    public void saveNew(Student s) throws Exception { 
     ArrayList<Student> students = peek(); 

     int editIndex = -1; 
     for(int i=0;i<students.size();i++){ 
      if (students.get(i).getID().equals(s.getID())){ 
       editIndex = i; 
       break; 
      } 
     } 

     if (editIndex != -1){ 
      students.set(editIndex, s); //replace 
     }else{ 
      students.add(s); //add 
     } 

     save(students); 
    } 
} 

其中

import java.io.Serializable; 


public class Student implements Serializable{ 

    private static final long serialVersionUID = 1L; 
    private String ID; 
    private String s; 

    public Student(String ID, String s) { 
     this.ID = ID; 
     this.s = s; 
    } 

    public String getID() { 
     return ID; 
    } 

    public void setID(String iD) { 
     ID = iD; 
    } 

    public String getS() { 
     return s; 
    } 

    public void setS(String s) { 
     this.s = s; 
    } 

    @Override 
    public String toString() { 
     return "Student [ID=" + ID + ", s=" + s + "]"; 
    } 


}