Java Library System Program - 保存程序

时间:2017-05-04 10:38:44

标签: java class csv save edit

我正在尝试将我的数据从我的程序保存到文件中,但它不会保存添加的书籍或学生。程序将运行,但不会保存任何数据,也不允许程序保存数据。

    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!");
        }

    }

1 个答案:

答案 0 :(得分:0)

没看到你写入文件的类是否实现了序列化。

如果检查此链接,也会注意到您正在使用的Java版本。现在硬编码文件路径和名称只是为了看它是否正常工作。

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

还要进行一些日志记录或控制台打印,以查看代码的位置。