/**
* 整行读取
*/
@Test
public void f2() {
FileInputStream fis = null;
String file = "H:\\abc\\123321.txt";
try {
fis = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fis, "gbk"); //最后的"GBK"根据文件属性而定,如果不行,改成"UTF-8"试试
BufferedReader br = new BufferedReader(reader);
String line;
int lineNum = 0;
int pointNum = 1;
List<Map<String, Map<String, Double>>> maps = new ArrayList<>();
Hashtable<Integer, Map<String, Double>> ht = new Hashtable<>();
while ((line = br.readLine()) != null) {
++lineNum;
line = line.replaceAll("\\s+", "");
if (line.equals("")) continue;
if (line.contains("XC=") && line.contains("X=")) {
HashMap<String, Double> map = new HashMap<>();
map.put("X", Double.valueOf(line.split("XC=")[1].split("X=")[0]));
lineNum++;
System.out.println(line);
line = br.readLine().replaceAll("\\s+", "");
map.put("Y", Double.valueOf(line.split("YC=")[1].split("Y=")[0]));
ht.put(pointNum, map);
pointNum++;
}
System.out.println(line);
}
System.out.println("lineNum = " + lineNum);
System.out.println("ht = " + ht);
ht.forEach((k, v) -> {
System.out.println(k + ":" + v);
});
br.close();
reader.close();
} catch (FileNotFoundException e) {
log.info("系统找不到指定文件:{}", file);
e.printStackTrace();
} catch (IOException e) {
log.info("文件处理异常。。。");
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
posted @ 2021-04-19 00:36  chenlublogs  阅读(80)  评论(0)    收藏  举报