gpt4 book ai didi

Java - 跨平台桌面(?)保存

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

目前我有这个:

            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();
}
}
}

最佳答案

下面的内容应该可以解决问题。

String path = System.getProperty("user.dir") + File.separator + "foo.txt";
File customfile = new File(path);

字符串路径 = System.getProperty("user.dir") + File.separator + "foo.txt";

获取用户当前所在目录

System.getProperty("user.dir")

然后使用

获取文件分隔符

文件.分隔符

最后命名要制作的文本文件

foo.txt

之后,

文件自定义文件 = new File(路径);

使用您刚刚创建的 path 变量创建新文件,该变量保存文件的放置位置。

关于Java - 跨平台桌面(?)保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27820570/

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