[英]Java: How do I access my properties file?
So I have a property file in my project. 所以我的项目中有一个属性文件。 I need to access it.
我需要访问它。
Here's the tree structure: 这是树形结构:
+ Project Name
|--+ folder1
|--+ propertyfolder
|--+ file.properties
Or: Project/propertyfolder/file.properties 或者:Project / propertyfolder / file.properties
Here's what I've tried so far (one at a time, not all at once): 到目前为止,这是我尝试过的操作(一次一次,并非一次全部):
// error: java.io.File.<init>(Unknown Source)
File file = new File(System.getProperty("file.properties"));
File file = new File(System.getProperty("propertyfolder/file.properties"));
File file = new File(System.getProperty("propertyfolder\\file.properties"));
File file = new File(System.getProperty("../../propertyfolder/file.properties"));
And: 和:
InputStream inputStream = getClass().getResourceAsStream("file.properties");
InputStream inputStream = getClass().getResourceAsStream("../../propertyfolder/file.properties");
InputStream inputStream = getClass().getResourceAsStream("propertyfolder/file.properties");
InputStream inputStream = getClass().getResourceAsStream("propertyfolder\\file.properties");
And all variations within getClass()
, such as getClass().getClassLoader()
, etc. 以及
getClass()
所有变体,例如getClass().getClassLoader()
等。
The error I'm getting is a NullReferenceException
. 我得到的错误是
NullReferenceException
。 It's not finding the file. 找不到文件。 How do I find it correctly?
我如何正确找到它?
(taken from comment to answer as OP suggested) (摘自评论,回答为OP建议)
Just use File file = new File("propertyfolder/file.properties")
but you do need to know where is java process working directory, if you cannot control it try an absolute path /c:/myapp/propertyfolder/file.properties . 只需使用
File file = new File("propertyfolder/file.properties")
但您确实需要知道Java进程工作目录在哪里,如果无法控制它,请尝试使用绝对路径/c:/myapp/propertyfolder/file.properties 。
You may also use /myapp/propertyfolder/file.properties path without C: disk letter to avoid windows-only mapping. 您也可以使用不带C:盘符的/myapp/propertyfolder/file.properties路径,以避免仅Windows映射。 You may use / path separator in Java apps works in Win,Linux,MacOSX.
您可以在Win,Linux,MacOSX中的Java应用程序中使用/路径分隔符。 Watch out for text file encoding, use InputStreamReader to given an encoding parameter.
注意文本文件的编码,请使用InputStreamReader赋予编码参数。
File file = new File("propertyfolder/file.properties");
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader reader = new BufferedReader(isr);
..read...
reader.close(); // this will close underlaying fileinputstream
Inorder to use getClass().resourceAsStream("file.properties")
you need to make sure the file is there in the classpath. 为了使用
getClass().resourceAsStream("file.properties")
您需要确保文件在类路径中。
That is if your Test.java file is compiled into bin/Test.class then make sure to have file.properties
in the bin/ folder along with the Test.class 也就是说,如果你的Test.java文件编译成BIN /的Test.class然后确保有
file.properties
在bin /文件夹中的Test.class一起
Otherwise you can use the Absolute Path, which is not advisable. 否则,您可以使用绝对路径,这是不可取的。
Did you set System properties to load file.properties from 您是否设置了系统属性以从中加载file.properties
1) Command line using -Dpropertyname=value OR 1)命令行使用-Dpropertyname = value或
2) System.setProperty() API OR 2)System.setProperty()API或
3) System.load(fileName) API? 3)System.load(fileName)API?
If you have n't done any one of them, do not use System.getProperty() to load file.properties file. 如果您尚未执行任何操作,请不要使用System.getProperty()加载file.properties文件。
Assuming that you have not done above three, the best way to create file InputStream is 假设您还没有完成上述三项操作,那么创建文件InputStream的最佳方法是
InputStream inputStream = getClass().getResourceAsStream("<file.properties path from classpath without />");
Properties extends Hashtable so, Each key and its corresponding value in the property list is a string. 属性扩展了Hashtable, 因此, 属性列表中的 每个键及其对应的值都是一个字符串。
Properties props = new Properties();
// File - Reads from Project Folder.
InputStream fileStream = new FileInputStream("applicationPATH.properties");
props.load(fileStream);
// Class Loader - Reades Form src Folder (Stand Alone application)
ClassLoader AppClassLoader = ReadPropertyFile.class.getClassLoader();
props.load(AppClassLoader.getResourceAsStream("classPATH.properties"));
for(String key : props.stringPropertyNames()) {
System.out.format("%s : %s \n", key, props.getProperty(key));
}
// Reads from src folder.
ResourceBundle rb = ResourceBundle.getBundle("resourcePATH");// resourcePATH.properties
Enumeration<String> keys = rb.getKeys();
while(keys.hasMoreElements()){
String key = keys.nextElement();
System.out.format(" %s = %s \n", key, rb.getString(key));
}
// Class Loader - WebApplication : src folder (or) /WEB-INF/classes/
ClassLoader WebappClassLoader = Thread.currentThread().getContextClassLoader();
props.load(WebappClassLoader.getResourceAsStream("webprops.properties"));
To read properties from specific folder. 从特定文件夹读取属性。 Construct path form ProjectName
构造路径表单ProjectName
InputStream fileStream = new FileInputStream("propertyfolder/file.properties");
If Key:value pairs specified in .txt file then, 如果在.txt文件中指定了“键:值”对,
public static void readTxtFile_KeyValues() throws IOException{
props.load(new FileReader("keyValue.txt") );
// Display all the values in the form of key value
for (String key : props.stringPropertyNames()) {
String value = props.getProperty(key);
System.out.println("Key = " + key + " \t Value = " + value);
}
props.clear();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.