【问题标题】:Creating files in Roaming Directory在漫游目录中创建文件
【发布时间】:2016-08-14 03:37:14
【问题描述】:

我在尝试在任何目录中的任何位置创建文件时都遇到了很多麻烦,而且我没有运气。

@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$6.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.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.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$4.run(Unknown Source)
    at java.awt.EventQueue$4.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)

【问题讨论】:

  • 您是否使用过其他工具来验证该路径是否确实存在?

标签: java file directory mkdir


【解决方案1】:

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

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

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

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

  2. 检查源代码和/或使用调试器运行您的代码,以确定您真正使用的 installModpack 方法。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这适用于使用 APPDATA 环境变量的操作系统。 Afaik 那只是 Windows。无论如何,它在 Linux 上不起作用。
    猜你喜欢
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    相关资源
    最近更新 更多