Java - 如果文本文件存在,则创建附加“1”的新文件
Java - If text file exists, create new file appending "1"
如何更改我的代码以查找现有的 "Library.txt" 文件,如果存在,则创建一个名为 "Library1" 的新文件,等等?
我目前有这个代码:
if(Menu.menuChoice == 7 && Library.ManualList.isEmpty()){
System.out.println(Messages.addManualFirst);
Menu.displayMenu();
}
else if(Menu.menuChoice == 7){
Library.displayManualList();
boolean saveYesNo = Console.readYesNo("\nThe manualKeeper® app is able to save your current library to a '.txt' \nfile in your workspace directory.\n\nWould you like to save the current library? (Y/N):\n");
if(saveYesNo){
try {
File file = new File("Library.txt");
file.delete();
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();
} catch (IOException e) {
System.out.println("Error! Library unable to save.");
}
System.out.println("\n\n--------------------------------------------------------------------------");
System.out.println("\n Library saved!\n");
System.out.println("--------------------------------------------------------------------------\n");
}
else if(saveYesNo){
System.out.println("\n");
}
Menu.displayMenu();
}
不要调用 file.delete()
,那样会删除现有的!我想你想要像
这样的东西
File file = new File("Library.txt");
// file.delete();
// file.createNewFile();
for (int i = 1; file.exists(); i++) {
file = new File(String.format("Library%d.txt", i));
}
你可以理解为声明 i
并将其初始化为 1
,而 file
存在时将 i
加一。将 file
(同时)重新初始化为 "Library#.txt" 的 String
,其中 #
被 i
替换。
您可以阅读格式化程序语法 here。
如何更改我的代码以查找现有的 "Library.txt" 文件,如果存在,则创建一个名为 "Library1" 的新文件,等等?
我目前有这个代码:
if(Menu.menuChoice == 7 && Library.ManualList.isEmpty()){
System.out.println(Messages.addManualFirst);
Menu.displayMenu();
}
else if(Menu.menuChoice == 7){
Library.displayManualList();
boolean saveYesNo = Console.readYesNo("\nThe manualKeeper® app is able to save your current library to a '.txt' \nfile in your workspace directory.\n\nWould you like to save the current library? (Y/N):\n");
if(saveYesNo){
try {
File file = new File("Library.txt");
file.delete();
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();
} catch (IOException e) {
System.out.println("Error! Library unable to save.");
}
System.out.println("\n\n--------------------------------------------------------------------------");
System.out.println("\n Library saved!\n");
System.out.println("--------------------------------------------------------------------------\n");
}
else if(saveYesNo){
System.out.println("\n");
}
Menu.displayMenu();
}
不要调用 file.delete()
,那样会删除现有的!我想你想要像
File file = new File("Library.txt");
// file.delete();
// file.createNewFile();
for (int i = 1; file.exists(); i++) {
file = new File(String.format("Library%d.txt", i));
}
你可以理解为声明 i
并将其初始化为 1
,而 file
存在时将 i
加一。将 file
(同时)重新初始化为 "Library#.txt" 的 String
,其中 #
被 i
替换。
您可以阅读格式化程序语法 here。