目前我有这个:
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 workspace directory.\n\nWould you like to save the current library? (Y/N):\n");
if(saveYesNo==true){
try {
File file = new File("Library.txt");
System.getProperty("user.home");
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();
}
}
}
它将文本文件保存在Eclipse的工作空间目录中,类文件位于该目录中。但是,当我将此项目导出到“.jar”文件时,我希望将文本文件保存在与jar文件相同的目录中或桌面上。我读过有关“User.home”的内容,但由于我是编程新手,因此无法使其工作。有谁知道我怎么能实现这个目标?
更新代码:
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 workspace directory.\n\nWould you like to save the current library? (Y/N):\n");
if(saveYesNo==true){
try {
String path = System.getProperty("user.dir") + File.separator + "Library.txt";
File file = new File(path);
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();
}
}
}
答案 0 :(得分:0)
以下应该可以解决问题。
String path = System.getProperty("user.dir") + File.separator + "foo.txt";
File customfile = new File(path);
String path = System.getProperty("user.dir") + File.separator + "foo.txt";
获取用户所在的当前目录
System.getProperty("user.dir")
然后使用
获取文件分隔符 File.separator
最后用
命名文本文件 foo.txt
。
之后,
File customfile = new File(path);
使用您刚刚创建的path
变量生成新文件,该变量保存文件的放置位置。