简体   繁体   English

Java-读写BIN文件中的数组

[英]Java - Reading & writing array to/from BIN file

I am working on a small library application which stores technical manuals in this format: 我正在使用一个小型图书馆应用程序,该应用程序以这种格式存储技术手册:

在此处输入图片说明

Currently, I am trying to enable the contents of the library to be saved and loaded on demand by the user, to a bin file. 当前,我正在尝试使该库的内容能够保存并根据用户的需要加载到bin文件中。

However when I try and load a library file, the only change is that "-1" is printed to the console. 但是,当我尝试加载库文件时,唯一的变化是在控制台上打印了“ -1”。 Shown here: 如图所示:

在此处输入图片说明

Ideally I would like this to store the loaded manuals into the users current library. 理想情况下,我希望将已加载的手册存储到用户的当前库中。

Here is my code: 这是我的代码:

//Choice 7: Load Library:

            if(Menu.menuChoice == 7){
                boolean loadYesNo = Console.readYesNo("\n\nThe manualKeeper app is able to load and display any 'Library.txt' files \nfound in your home folder directory.\n\nWould you like to load and display library? (Y/N):\n");
                String fileName = "Library.bin";
                if(loadYesNo==true){
                try {
                    FileInputStream fileIs = new FileInputStream(fileName);
                    ObjectInputStream is = new ObjectInputStream(fileIs);
                    Object x = is.read();
                    System.out.println(x);
                    is.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Menu.displayMenu();
                }
                else if(loadYesNo==false){
                    System.out.println("\n\n--------------------------------------------------------------------------");
                    System.out.println("\n                             Library not loaded!\n");
                    System.out.println("--------------------------------------------------------------------------\n");
                    Menu.displayMenu();
                }
            }

//Choice 0: Exit the program:

            if(Menu.menuChoice == 0){
                if(Menu.menuChoice == 0){
                    if(Library.ManualList.size() > 0){
                        boolean saveYesNo = Console.readYesNo("\nThe manualKeeper app is able to save your current library to a '.txt' \nfile in your home folder directory (C:\\Users\\ 'YOUR NAME').\n\nWould you like to save the current library? (Y/N):\n");
                        String fileName = "Library.bin";
                        if(saveYesNo==true){
                            try {
                                FileOutputStream fileOs = new FileOutputStream(fileName);
                                ObjectOutputStream os = new ObjectOutputStream(fileOs);
                                for (int i = 0; i < Library.ManualList.size(); i++){
                                    os.writeObject(Library.ManualList.get(i).displayManual());
                                    os.close();
                                }
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            System.out.println("DONE WRITING!");
                        }
                            else if(saveYesNo==false){
                                System.out.println("\n\n--------------------------------------------------------------------------");
                                System.out.println("\n                              Library not saved!\n");
                                System.out.println("--------------------------------------------------------------------------\n");
                                break exit;
                        }
                        Menu.displayMenu();
                    }else if(Library.ManualList.isEmpty()){ 
                        Menu.displayMenu();
                    }
                }
            }               

        }
    System.out.println("\n              ~   You have exited the manualKeeper app!   ~                  ");
    System.out.println("\n                  Developed by Oscar Moore - 2014 - UWL\n");
    System.out.println("\n                                   <3\n");

}
}

If more code needs to be shown please let me know. 如果需要显示更多代码,请告诉我。 Thank you. 谢谢。

The lines: 这些行:

Object x = is.read(); System.out.println(x);

are resulting in the -1 you're seeing in the console. 导致您在控制台中看到的-1。

From the javadocs ( link ), read() returns: 从javadocs( link ), read()返回:

-1 if the end of the stream is reached. 如果到达流的末尾,则为-1。

So, first, check that Library.bin is being saved correctly (use a text editor after saving). 因此,首先,检查Library.bin是否正确保存(保存后使用文本编辑器)。

Then, make sure the file you are reading in is the same exact file. 然后,确保要读取的文件是相同的文件。

It looks like you aren't using a full path to the file. 看来您没有使用文件的完整路径。 eg Library.bin versus /home/project/Library.bin . 例如Library.bin/home/project/Library.bin Try using the full path. 尝试使用完整路径。 I suspect that is the problem. 我怀疑这是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM