通过从另一个文件逐行读取来加载属性文件 [英] Loading a property file by reading line by line from another file
问题描述
我正在读取一个名为 abc.txt 的文件,abc.txt 的每一行都是一个属性文件.例如:-
I am reading a file called abc.txt and each line of abc.txt is an properties file. For eg:-
label.properties
label_ch.properties
label_da.properties
label_de.properties
label_en.properties
所以在阅读每一行后,我在字符串行中获取属性文件,然后我尝试加载该属性文件,但没有加载.我的实现有什么问题吗?
So after reading each line I am getting properties file in String line, and after that I am trying to load that properties file but it is not getting loaded. Any thing wrong with my implementation?
这是我的代码-
package testing.project;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Properties;
public class Project {
public static void main(String[] args) {
BufferedReader br = null;
HashMap<String, String> hashMap = new LinkedHashMap<String, String>();
try {
br = new BufferedReader(new FileReader("C:\\apps\\apache\\tomcat7\\webapps\\examples\\WEB-INF\\classes\\abc.txt"));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
loadProperties(line);
while (br.readLine() != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
String everything = sb.toString();
System.out.println(everything);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void loadProperties(String line) {
Properties prop = new Properties();
InputStream in = Project.class.getResourceAsStream(line);
try {
//As soon as it gets into prop.load(in), cursor goes to br.close that is in main method.
prop.load(in);
for(Object str: prop.keySet()) {
Object value = prop.getProperty((String) str);
System.out.println(str+" - "+value);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我收到错误 -
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
推荐答案
仔细检查你的代码,除了上面提到的那些之外,一切都很好.而不是写
Going through your code it appears all is good except for the ones mentioned above as well. Instead of writing
String line = br.readLine();
loadProperties(line);
while (br.readLine() != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
喜欢写
String line = null;
while ((line = br.readLine()) != null)
{
loadProperties(line);
sb.append(line);
sb.append("\n");
line = br.readLine();
}
此外,由于是java代码,所以在描述文件路径时,最好用正斜杠(/)而不是两个反斜杠(\).
Moreover, since it's a java code, so prefer to put forward slash (/) instead of two backslashes(\), while describing a path to a file.
例如:
BufferedReader br = new BufferedReader(new FileReader("C:/Apache/tomcat/webapps/GaganIsOnline/WEB-INF/classes/names.txt"));
并且请检查文件路径是否正确,意思是说,该文件 abc.txt 确实存在于该给定位置.
And please do check, if the file path is proper, mean to say, does the said file abc.txt do exists in that given location.
问候.
这篇关于通过从另一个文件逐行读取来加载属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!