标签:stringbu sample pen 中文 log tom red 方案 input
读取编码为gb2312格式的文件,出现中文乱码
普通文件读取,通常使用FileReader
try (FileReader fileReader = new FileReader(sampleFile);
BufferedReader br = new BufferedReader(fileReader);) {
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);//中文乱码,文件格式是gb2312
}
}
解决方案:
用InputStreamReader包装一层,并指定字符集
try(FileInputStream fis = new FileInputStream(sampleFile); InputStreamReader isr = new InputStreamReader(fis, "gb2312"); BufferedReader br = new BufferedReader(isr);){ String line = null; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line);//不是乱码 https://www.cnblogs.com/tomcatandjerry } }
标签:stringbu sample pen 中文 log tom red 方案 input
原文地址:https://www.cnblogs.com/tomcatandjerry/p/10119699.html