读取文章和打印

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public static void main(String[] args) {
String filePath = "D:\\黄帝内经白话文.txt";
try {
FileInputStream fis = new FileInputStream(filePath);
InputStreamReader isr = new InputStreamReader(fis,detectEncoding(filePath));
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}



public static String detectEncoding(String filePath) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)))){
br.mark(1000);
String line = br.readLine();
if (line != null) {
if (line.startsWith("\uFEFF")) {
return "UTF-8";
} else if (line.startsWith("\uFFFE")) {
return "UTF-16LE";
} else if (line.startsWith("\u0000\uFEFF")) {
return "UTF-16BE";
}
}
return "GBK";
}
}
posted @ 2024-04-29 17:32  皇问天  阅读(2)  评论(0编辑  收藏  举报