gpt4 book ai didi

Java - 从 BIN 文件读取数组或将数组写入 BIN 文件

转载 作者:太空宇宙 更新时间:2023-11-04 14:15:06 32 4
gpt4 key购买 nike

我正在开发一个小型图书馆应用程序,它以这种格式存储技术手册:

enter image description here

目前,我正在尝试将库的内容保存并根据用户的需要加载到 bin 文件中。

但是,当我尝试加载库文件时,唯一的变化是“-1”被打印到控制台。此处显示:

enter image description here

理想情况下,我希望将加载的手册存储到用户当前的库中。

这是我的代码:

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

}
}

如果需要显示更多代码,请告诉我。谢谢。

最佳答案

行:

对象 x = is.read();
System.out.println(x);

结果是您在控制台中看到的 -1。

从 javadocs ( link ) 中,read() 返回:

-1 if the end of the stream is reached.

因此,首先检查 Library.bin 是否已正确保存(保存后使用文本编辑器)。

然后,确保您正在读取的文件是同一个文件。

您似乎没有使用文件的完整路径。例如Library.bin/home/project/Library.bin。尝试使用完整路径。我怀疑这就是问题所在。

关于Java - 从 BIN 文件读取数组或将数组写入 BIN 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27848832/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com