如何在java中阅读这种文本文件,请帮忙... 这个文本文件用作我的java程序的模板我试图读取这个文件它显示文本文件中的任何内容,但我想放置那些{0} ...哪个java内容。
Q799,B080+000
q831
rN
S4
D7
ZT
JF
OD,P
R24,0
N
X555,56,2,780,714
A771,73,1,1,2,1,N,"A {0}"
A742,70,1,1,2,2,N," {1}({31})"
A765,450,1,1,2,2,N,"SCREEN-{38}"
A706,86,1,2,1,1,N,"Adm :Rs.{6}"
A682,86,1,2,1,1,N,"E.TAX :Rs.{28}"
答案 0 :(得分:2)
在这里尝试以下代码:
Object obj = parser.parse(new FileReader("D:\\CountryJSONFile.json"));
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jsonObject = (JSONObject)obj;
Iterator<?> keys = jsonObject .keys();
while( keys.hasNext() ){
String key = (String)keys.next();
String value = jsonObject.getString(key);
map.put(key, value);
}
答案 1 :(得分:0)
考虑使用Maps
来存储键值对而不是数组: -
HashMap<String,String> jsonMap = new HashMap<String,String>();
jsonMap.put("Name",(String) jsonObject.get("Name"));
jsonMap.put("ShowTime",(String) jsonObject.get("ShowTime"));
jsonMap.put("Total",(String) jsonObject.get("Total"));
答案 2 :(得分:0)
private String readFile(String folder, String file) {
logger.info("Read file ...");
Stream<String> lines = null;
try {
lines = Files.lines(Paths.get(folder, file));
} catch (IOException e) {
e.printStackTrace();
}
String txt = lines.map(Object::toString).collect(Collectors.joining(""));
lines.close();
logger.info("File is ready");
return txt;
}
private User jsonToProductTypes(String json){
Gson gson = new Gson();
return gson.fromJson(json, User.class);
}
User is a class with the same structure as the json
User can contain other objects , after you have a java object you can do what ever you want