gpt4 book ai didi

java - 在 protected 文件夹中创建文件

转载 作者:行者123 更新时间:2023-11-29 09:04:01 24 4
gpt4 key购买 nike

我正在尝试在最有可能受到保护的文件夹中创建一个文件夹(以及其中的一个文件)。

这是我的代码:

package me.pogostick29.audiorpg.data;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class DataManager {

private DataManager() { }

private static DataManager instance = new DataManager();

public static DataManager getInstance() {
return instance;
}

private File folder;
private File settings;

public void setup() throws IOException {

String foldername;
String osname = System.getProperty("os.name").toLowerCase();

if (osname.startsWith("mac")) foldername = System.getProperty("user.home") + "/Library/Application Support/AudioRPG";
else if (osname.startsWith("linux")) foldername = System.getProperty("user.home") + "/.AudioRPG/";
else if (osname.startsWith("win")) foldername = System.getenv("APPDATA") + "\\.AudioRPG\\";
else throw new RuntimeException("Unknown OS: " + osname);

folder = new File(foldername);

if (folder.exists()) {

boolean success = false;

try { success = folder.mkdirs(); }
catch (SecurityException e) { FileUtils.forceMkdir(folder); }

if (!success) throw new IOException("Could not create AudioRPG folder.");
}

settings = new File(folder, "settings.txt");

if (!settings.exists()) {

boolean success = false;

try { success = settings.createNewFile(); }
catch (SecurityException e) { throw new IOException("Could not create settings file."); }

if (!success) throw new IOException("Could not create settings file.");
}
}
}

当我运行代码(在 Mac 上)时,我得到了这个:

Exception in thread "main" java.io.IOException: No such file or directory at java.io.UnixFileSystem.createFileExclusively(Native Method) at java.io.File.createNewFile(File.java:883) at me.pogostick29.audiorpg.data.DataManager.setup(DataManager.java:55) at me.pogostick29.audiorpg.AudioRPG.main(AudioRPG.java:26)

如何在此位置创建文件夹/文件?

最佳答案

我使用 FileSystemView.getFileSystemView().getHomeDirectory(); 获取主目录。现在可以使用了!

关于java - 在 protected 文件夹中创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15937682/

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