Java - 将输出更改为二进制格式以便稍后读取

Java - Change output to binary format to be read later

最近我一直在开发一个技术手册的图书馆应用程序,我已经设法让我的应用程序将存储的手册保存到 txt 文件,但到目前为止还无法将这些文本文件加载回我的图书馆文本文件的内容只是打印到控制台。

我听说在 java 中允许读取和加载二进制文件更容易,所以我的问题是是否有一种方法可以更改我现有的代码以读取和写入二进制文件,因此手册实际上会恢复到用户加载 library.txt 文件时的库。

这是我的代码:

//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");
                if(loadYesNo==true){
                try {
                    FileReader fr = new FileReader("Library.txt");
                    BufferedReader br = new BufferedReader(fr);

                    String str;
                    while ((str = br.readLine()) != null) {
                        System.out.println(str + "\n");
                    }

                    br.close();
                } catch (IOException e) {
                    System.out.println("\n--------------------------------------------------------------------------");
                    System.out.println("\n                    Error! No previous libraries found.");
                    System.out.println("\n--------------------------------------------------------------------------\n");
                }

                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();
                }
            }

...
                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");
                        if(saveYesNo==true){
                            try {
                                File file = new File("Library.txt");

                                file.createNewFile();
                                FileWriter fw = new FileWriter(file, true);
                                PrintWriter pw = new PrintWriter(fw);

                                for (int i = 0; i < Library.ManualList.size(); i++){
                                    pw.println("\n-------------------- Index Number: " + i + " --------------------");
                                    pw.println(Library.ManualList.get(i).displayManual());
                                    pw.println("---------------------------------------------------------");
                                }
                                pw.close();
                                System.out.println("\n\n--------------------------------------------------------------------------");
                                System.out.println("\n                              Library saved!\n");
                                System.out.println("--------------------------------------------------------------------------\n");
                                break exit;
                            } catch (IOException e) {
                                System.out.println("Error! Library unable to save.");
                            }

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

}
}

我是 Java 的新手,所以如果我的代码有任何错误,请原谅我:)

首先请记住,磁盘上的所有数据都是二进制的。你如何阅读它是 encoding/decoding 这些字节的问题。如果您查看 BufferedReader 的 JavaDoc,您会看到描述为 "Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines."

因此 BufferedReader 已设置为处理(或解码)文本。该文本可以是 ASCII 或 UTF-8 或任何编码...

现在您要做的是以二进制形式读取数据。这实际上要简单得多。方法如下

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;

Path libraryFile = Paths.get("path/to/Library.txt");
byte[] data = Files.readAllBytes(libraryFile);