java java属性文件中的当前目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/977022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Current directory in java properties file
提问by Lehane
Is there any way of specifying the current directory in a java properties file?
有没有办法在 java 属性文件中指定当前目录?
i.e. something like:
即类似:
fileLocation={currentDir}/fileName.txt
回答by Michael Borgwardt
No. Properties files do not have any builtin macro facilities. You can programmatically get the currect directory of the user running the Java app through the user.dir system property.
否。属性文件没有任何内置的宏工具。您可以通过user.dir 系统属性以编程方式获取运行 Java 应用程序的用户的当前目录。
回答by cloudhead
I'm pretty sure it just defaults to the current directory, if not you can do
我很确定它只是默认为当前目录,否则你可以这样做
fileLocation="./fileName.txt"
回答by Georg Leber
I don't know any direct solution for this problem. You can load the URL to the properties file and then load the filename from that file:
我不知道这个问题的任何直接解决方案。您可以将 URL 加载到属性文件,然后从该文件加载文件名:
ClassLoader loader = YourClass.class.getClassLoader();
URL resourceURL =loader.getResource("check.properties");
String fileToLoad = resourceURL.getPath() + <fileNameFromPropertyFile>;

