Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-将输出更改为二进制格式,以便以后读取_Java_Input_Binary_Output - Fatal编程技术网

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

Java-将输出更改为二进制格式,以便以后读取,java,input,binary,output,Java,Input,Binary,Output,最近,我一直在为技术手册开发一个库应用程序,我已成功地允许我的应用程序将存储的手册保存为txt文件,但无法将这些文本文件加载回我的库中,到目前为止,文本文件的内容只是打印到控制台上 我听说在java中读取和加载二进制文件更容易,所以我的问题是,是否有办法更改现有代码以读取和写入二进制文件,这样当用户加载library.txt文件时,手册实际上会恢复到库中 这是我的密码: //Choice 7: Load Library: if(Menu.menuChoice == 7)

最近,我一直在为技术手册开发一个库应用程序,我已成功地允许我的应用程序将存储的手册保存为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");

}
}
//选项7:加载库:
如果(Menu.menuChoice==7){
boolean loadYesNo=Console.readYesNo(“\n\nManualKeeper应用程序能够加载和显示在主文件夹目录中找到的\n任何“Library.txt”文件。\n\n是否加载和显示库?(Y/n):\n”);
如果(loadYesNo==true){
试一试{
FileReader fr=新的FileReader(“Library.txt”);
BufferedReader br=新的BufferedReader(fr);
字符串str;
而((str=br.readLine())!=null){
System.out.println(str+“\n”);
}
br.close();
}捕获(IOE异常){
System.out.println(“\n-----------------------------------------------------------------------------------------------”);
System.out.println(“\n错误!未找到以前的库。”);
System.out.println(“\n----------------------------------------------------------------------------------------------\n”);
}
Menu.displayMenu();
}
else if(loadYesNo==false){
System.out.println(“\n\n-----------------------------------------------------------------------------------------------------”);
System.out.println(“\n库未加载!\n”);
System.out.println(“-------------------------------------------------------------------------------------\n”);
Menu.displayMenu();
}
}
...
如果(Menu.menuChoice==0){
如果(Menu.menuChoice==0){
如果(Library.ManualList.size()>0){
boolean saveYesNo=Console.readYesNo(“\nManualKeeper应用程序能够将当前库保存到主文件夹目录中的“.txt”\n文件(C:\\Users\\\'your NAME”)。\n\n是否保存当前库?(Y/n):\n”);
if(saveYesNo==true){
试一试{
File File=新文件(“Library.txt”);
createNewFile();
FileWriter fw=新的FileWriter(文件,true);
PrintWriter pw=新的PrintWriter(fw);
对于(int i=0;iSystem.out.println(“\n首先请记住,磁盘上的所有数据都是二进制的。如何读取这些数据是对这些字节进行编码/解码的问题。如果查看BufferedReader的JavaDoc,您将看到描述中说”从字符输入流读取文本,缓冲字符,以便有效读取字符、数组和行。”

所以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);

不知道为什么这被否决,因为它清楚地回答了从磁盘读取二进制数据的问题