dat文件数据读取为map(java)
dat文件的数据格式和json不一样,python拥有库进行文件解析
博主找了半天,也没找到java相关的处理包
所以自己写了个工具类来使用
本工具类适用于博主项目
其他小伙伴需要使用的话,可能需要更改一下代码
其中path为读取的dat文件路径
public static Map readDat(String path) {
BufferedReader br = null;
Map hashMap = new HashMap<String, String>();
try {
File file = new File(path); // java.io.File
FileReader fr = new FileReader(file); // java.io.FileReader
br = new BufferedReader(fr); // java.io.BufferedReader
String line;
boolean flag = false;
String value = "";
String key = "";
while ((line = br.readLine()) != null) {
line = line.replaceAll(" ", "");
if (!"".equals(key) && line.contains("=")) {
hashMap.put(key, value);
}
if (line.contains("&")) {
continue;
}
if (line.contains("=")) {
String[] split = line.split("=");
key = split[0];
value = split[1];
}
if (!line.contains("=") && !line.contains("&")) {
if (!line.contains("/")) {
value += line;
}
hashMap.put(key, value);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return hashMap;
}

浙公网安备 33010602011771号