2017-05-04 49 views
0

我試圖將我的數據從我的程序保存到文件中,但它不會保存添加的書籍或學生。該程序將運行,但不會保存任何數據,也不會允許程序保存數據。Java庫系統程序 - 保存程序

private static void addBook() { 
     // TODO Auto-generated method stub 
     int isbn, numbercopies; 
     String title, author, publisher; 


     System.out.println("\nEnter Title: "); 
     title = in.next(); 

     System.out.println("\nEnter Author: "); 
     author = in.next(); 

     System.out.println("\nEnter Publisher: "); 
     publisher = in.next(); 

     System.out.println("\nEnter ISBN: "); 
     isbn = in.nextInt(); 

     System.out.println("\nEnter Number of Copies:"); 
     numbercopies = in.nextInt(); 


// creating book object 

     Book b = new Book(isbn, numbercopies, title, author, publisher); 

// adding book to library via method 

     lib.addBook(b); 
    } 

    private static void addStudent(){ 

     int sID, age; 
     String FirstName, LastName; 




     System.out.println("\nEnter Full Name: "); 
     LastName = in.nextLine(); 
     FirstName = in.nextLine(); 

     System.out.println("\nEnter Age: "); 
     age = in.nextInt(); 

     System.out.println("\nEnter Student ID:"); 
     sID = in.nextInt(); 

     Students s = new Students(age, sID, FirstName, LastName); 

// adding student to student library 

     slib.addStudent(s); 

    } 

// method to save and quit 


    private static void saveAndQuit() { 
     // TODO Auto-generated method stub 
     System.out.println("Enter file name for Student: "); 
     fileName = in.next(); 

// stop the program from running with the boolean, through break   
     running = false; 

// writing to file  
     FileOutputStream fos = null; 
     ObjectOutputStream out = null; 


     try { 

      fos = new FileOutputStream(fileName); 
      out = new ObjectOutputStream(fos); 
      out.writeObject(slib); 



//closing the stream    
      fos.close(); 
      out.close(); 

     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 


// getting the file input 

    private static void loadScript(String name) { 
     // TODO Auto-generated method stub 
     FileInputStream fis = null; 
     ObjectInputStream in = null; 
     File file = new File(name); 
     if (file.exists()) { 
      try { 
       fis = new FileInputStream(file); 
       in = new ObjectInputStream(fis); 
       lib = (Library) in.readObject(); 
       fis.close(); 
       in.close(); 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ClassNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } else { 
      System.out.println("\nThe file does not exist!"); 
     } 

    } 
+0

通常在像Stackoverflow這樣的論壇上,成員爲編碼器遇到的問題提供瞭解決方案。好帖子是發佈你已經嘗試過的以及你的錯誤在哪裏。例如「刪除一本書」的代碼和哪部分似乎失敗了。這將有助於會員儘快爲您提供幫助。此外,成員還會將此類帖子看作未嘗試過的人,並希望statckoverflow用戶爲其編寫代碼。發佈您的刪除或保存代碼以及您嘗試的內容。不是整個程序都很難提供幫助。成員需要看到你做了你的研究。 – Renier

+0

對不起,我已編輯帖子 –

+0

它是否陷入困境?它是否創建任何文件或文件是空白的? – Renier

回答

0

沒有看到您正在寫入文件的類是否實現了序列化。

如果是勾選此鏈接,還會注意到您正在使用的Java版本。現在硬編碼的文件路徑和名稱只是爲了看看它的工作。

https://www.mkyong.com/java/how-to-write-an-object-to-file-in-java/

也把一些日誌或控制檯打印只是爲了看看那裏的代碼去。

+0

我試試這個,看看它是否工作,但我認爲我的保存方法有問題 –

+0

只要確保對象「slib」是序列化或實現Serializable 。 – Renier