2017-11-15 1 views
-1

이것은 내 saveTable 버튼으로 내 테이블의 한 행을 저장하고 내 Auto.auto 파일에 저장하지만, object(row)Auto type 중 하나만 저장할 수 있습니다. 때마다 나는 다른 line(row) 저장할 때마다 내 새 프로그램을 실행하는 동안 5 번처럼 저장 버튼을 클릭 한 후 내 파일에 object(row of table) 하나만 가지고 그래서 그것은 새로운 한 whith 오래 된 하나를 대체합니다. 이 문제를 어떻게 해결할 수 있습니까?내 프로그램을 실행할 때마다 내 파일에 개체를 추가하는 방법은 무엇입니까?

 saveTable.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      int row = Integer.parseInt(textField.getText()); 
      getText.getAuto(row); 
      File tabel = new File("C:\\Users\\Jovan\\Desktop\\Auto.auto"); 
      try { 
       if (!(tabel.exists())) { 
        tabel.createNewFile(); 
       } 
       FileOutputStream fos = new FileOutputStream(tabel); 
       ObjectOutputStream oos1 = new ObjectOutputStream(fos); 
       oos1.writeObject(getText.getAuto(row)); 
       oos1.close(); 


       ObjectOutputStream os2 = new ObjectOutputStream(new FileOutputStream(tabel, true)) { 
        protected void writeStreamHeader() throws IOException { 
         reset(); 
        } 
       }; 

       os2.writeObject(getText.getAuto(row)); 
       os2.close(); 




      } catch (IOException error) { 
       error.printStackTrace(); 
      } 
     } 


    }); 
+0

무엇에 대해'fos'? 그것은'new FileOutputStream (tabel, true);이 아니어야합니까? –

답변

1
new FileOutputStream(tabel, true) 

두 번째 매개 변수는 당신이

공공의 FileOutputStream (파일 파일, 부울 추가)합니다 가 FileNotFoundException이를 throw가

가를 작성 덮어 쓰는 대신 해당 파일에 추가된다는 것을 의미합니다 지정된 File 객체가 나타내는 파일에 기록하기위한 파일 출력 스트림. 두 번째 인수가 true 인 경우 바이트는 시작 부분이 아닌 파일의 끝에 끝에 기록됩니다. 이 파일 연결을 나타내는 새로운 FileDescriptor 객체는 입니다. 우선, 시큐리티 매니저가 존재하는 경우, 파일의 인수로 나타내지는 패스 를 인수로서 사용해, checkWrite 메소드가 불려갑니다.

파일이 있지만 일반 파일이 아닌 디렉토리 인 경우 은 존재하지 않지만 만들 수 없거나 다른 이유로 열 수 없으면 FileNotFoundException이 발생합니다.

참조 here

관련 문제