在 Java 项目中保存配置文件的位置

Where to save a configuration file in a Java project

所以我需要客户端在本地存储服务器地址等...并将其加密,但是文件应该保存在哪里,因为我尝试将它放在 Program Files 文件夹中,但出现错误。它适用于 mac 但不适用于 windows。

 /**
     * Gets the path to the configuration file
     * dependent on the operating system.
     * 
     * @return File Path {@code File}
     */
    public File getOSConfigurationPath () {

        File file;
        if (System.getProperty("os.name").startsWith("Windows")) {
            file = new File (System.getProperty("user.home") + "/Program Files/AutoSales/Configuration.txt");
        } else {
            file = new File (System.getProperty("user.home") + "/Library/Application Support/AutoSales/Configuration.txt");
        }

        return file;
    }

返回^^^^的文件是这样用的

} else {
                file.createNewFile();
                System.out.println("New Config File Created");
            }

出于某种原因,Windows 保存路径出现此错误。

如有任何建议,我们将不胜感激。谢谢

此异常是在您获取 Windows 文件位置的那一行发生的,还是在之后的某个时间点发生的?通常这样的异常意味着你的代码被执行 不止一次 并且你得到递归重复调用最终导致堆栈溢出。

这里要做的第一件事是调试代码,看看这个异常到底是什么时候发生的。隔离代码后,您将能够解决问题。

另请注意 - 您在调用 UI 相关操作时是否使用了 SwingUtilities.invokeLater(Runnable)?当你处理 Java Swing UI.

时,这是必须的

http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)

http://www.javamex.com/tutorials/threads/invokelater.shtml