2017-05-04 1 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 회원과 같은 포럼에서는 코더가 겪고있는 문제에 대한 해결책을 제시합니다. 좋은 게시글은 내가 시도한 내용과 오류가 발생한 부분을 게시하는 것입니다. 예를 들어 "책 삭제"코드와 어떤 부분이 실패한 것 같습니까? 이것은 회원들이 최대한 빨리 당신을 도울 수 있도록 도와줍니다. 또한 회원은 이와 같은 게시물을 statkoverflow 사용자가 코드를 작성하려고 시도하지 않은 사람으로 보았습니다. 게시물을 삭제하거나 저장하고 코드를 저장하십시오. 모든 프로그램이 도움을주는 것은 어렵습니다. 회원은 귀하가 귀하의 연구를 수행했는지 확인해야합니다. – Renier

+0

미안 해요 그 게시물을 편집했습니다 –

+0

잡기로 이동합니까? 파일을 만들거나 파일이 비어 있습니까? – Renier

답변

0

파일에 쓰는 클래스가 직렬화를 구현하는지 확인하지 못했습니다.

이 링크를 확인한 경우 사용중인 Java 버전도 확인하십시오. 지금은 파일 경로와 이름을 하드 코딩하여 작동하는지 확인하십시오.

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

또한 코드가 어디로 단지보고 일부 로그 또는 콘솔 인쇄에 넣어.

+0

아쉽습니다. 작동하는지는 알지만 내 저장 방법에 문제가 있다고 생각합니다. –

+0

"slib"개체가 serialize되어 있는지 또는 Serializable을 구현하는지 확인하십시오. . – Renier

관련 문제