我有一个小型 Java 应用程序,可以在设定的位置更新文件。该应用程序从 UpdateFiles 文件夹中读取,将此文件夹中的文件移动到相应的文件夹中。该应用程序有效。我想知道的是,如果我希望应用程序始终尝试在与应用程序相同的位置查找 UpdateFiles 文件夹,我应该如何设置路径?例如,如果应用程序是从桌面执行的,我希望它在桌面中查找 UpdateFiles 文件夹,与任何其他位置相同。该应用程序在 Windows 和 AIX/linux 环境中运行
谢谢/亲切的问候
You can access the system property that contains the current working directory:
String currentDirectory = System.getProperty("user.dir");
You can determine the location of the jar file, but this is not the same as the current working directory (the directory you get if you don't specify a path)
However the location of the jar is rarely a good place to store files and you don't want to mix released programs with files as this make upgrading your software much more complex.
I suggest using the user's home directory System.getProperties("user.home");
or a sub-directory This doesn't change if you upgrade you software is should always be writable.
您必须从 UpdateFiles 所在的目录启动您的应用程序,如有必要,您可以创建一个 bat 文件(适用于 Windows)并从任何位置运行它
cd c:\path_to_UpdateFiles
java MyApp
在 Linux 中做类似的事情