我正在尝试在 Java 中模拟另存为功能。我想为它选择一个文件名,因为我之前所做的代码只保存到
myData.dat
这在我的 Main.Class 的菜单中使用,它将查找
else if (option.compareTo("8") == 0){
manualLib.save();}
public void save(){
String content = "";
for (int i = 0; i < library.size(); i++){
for (int bar = 0; bar < library.get(i).size(); bar++){
content += library.get(i).get(bar).getSerial() + "\n";
content += library.get(i).get(bar).getTitle() + "\n";
content += library.get(i).get(bar).getAuthor() + "\n";
content += library.get(i).get(bar).onLoan() + "\n";
content += library.get(i).get(bar).getBorrower() + "\n";
}
}
Writer output;
try {
output = new BufferedWriter(new FileWriter("myData.dat"));
try {
output.write(content);
}
finally {
output.close();
System.out.println("Successfully saved to myData.dat file.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
实现这一目标的好方法是什么?