作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在最有可能受到保护的文件夹中创建一个文件夹(以及其中的一个文件)。
这是我的代码:
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/
我是一名优秀的程序员,十分优秀!