在漫游目录中创建文件

Creating files in Roaming Directory

我在尝试在任何目录中的任何位置创建文件时遇到了很多麻烦,但我一直没有成功。

@SuppressWarnings("resource") //Install the modpack and create files
public void installModpack(){
    File f = new File("\ultima");
    f.mkdirs();
}

这是堆栈跟踪:

C:\Users\Drew\AppData\Roaming
java.io.IOException: The system cannot find the path specified
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(Unknown Source)
    at ultima.launcher.Ultima.installModpack(Ultima.java:299)
    at ultima.launcher.Ultima.mouseClicked(Ultima.java:252)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access0(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

堆栈跟踪与您问题中的代码不匹配。堆栈跟踪表明 installModpack 正在调用 File.createNewFile,但在您的代码中没有这样的调用。

异常消息似乎在说它正在尝试创建 "C:\Users\Drew\AppData\Roaming",或在该目录中创建文件。这与您显然尝试使用的路径名“\ultima”不匹配。

没有看到实际代码,我们无法告诉您真正的解决方案是什么,但我会:

  1. 使用其他工具检查 "C:\Users\Drew\AppData\Roaming" 是否存在。

  2. 检查源代码 and/or 运行 您的代码使用调试器找出您真正使用的 installModpack 方法。

这是我自己回答的。我后来在代码中的一些代码弄乱了这个:p 任何正在看这里的人的正确用法是: File f = new File(System.getenv("APPDATA") + "/.ultima"); f.mkdirs();