gpt4 book ai didi

Java属性文件保存在system32文件夹中

转载 作者:行者123 更新时间:2023-12-01 11:18:54 25 4
gpt4 key购买 nike

所以,我有一种方法可以将一些数据保存在属性文件中,但发生了一些奇怪的事情。看,假设我的桌面上有 JAR 文件。如果我直接从那里打开它(双击等),属性文件将按应有的方式保存在桌面中。但是,如果将 JAR 拖到 Windows 启动列表并从那里打开它,属性文件将保存在 System32 文件夹中。

方法如下:

private void saveAncientsData() {
Properties prop = new Properties();
OutputStream output = null;

try {

output = new FileOutputStream("ancients.data");
File file = new File("ancients.data");
// set the properties value
for (int x = 0; x < currentLvlSpinnerFields.size(); x++) {
prop.setProperty(ancientNames[x], currentLvlSpinnerFields.get(ancientNames[x]).getValue().toString());
}

// save properties to project root folder
prop.store(output, null);
JOptionPane.showMessageDialog(this, "Data successfully saved in \n\n" + file.getCanonicalPath(), "Saved", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

非常感谢任何帮助,因为我一无所知。

提前致谢!

最佳答案

根据您的代码,您没有给出要在桌面中创建的属性文件的路径。

output = new FileOutputStream("ancients.data");

因此您的属性文件将在您的 jar 文件所在的同一目录中创建。

但是,如果您从父进程运行此 .jar 文件,则您的 jar 文件将在该父进程所在的目录中创建。

我猜当Windows启动时,win32目录中存在一个特定的进程来执行启动程序。我认为它是userinit.exe 。因此您的 prop 文件将在 System32 目录中创建。

如果您希望在桌面上创建属性文件,您可以将 jar 文件放在桌面上并向 .jar 添加快捷方式,或者您可以提供桌面的完整路径,例如

output = new FileOutputStream(System.getProperty("user.home") + "/Desktop/"+"ancients.data");

编辑

理解这个问题

1) 在桌面上创建一个名为 example 的文件夹。然后创建 2 个文件夹 path1path2 。然后将.jar添加到path1文件夹

enter image description here

2) 双击 path1 中的 jar。将在 path1 中创建一个属性文件,如您所料。

enter image description here

3) 删除属性文件。在 path2 中打开命令提示符。运行 path1 中的 Prop.jar 文件。输入 call "pathtodesktop/example/path1/Prop.jar" 按 Enter 键。

.property 文件将在 path2 而不是 path1 中创建,这就是您的情况。

enter image description here

关于Java属性文件保存在system32文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31487859/

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