不可行的代码
private String getText(String filePath) throws Exception {
File file = new File(filePath);
FileReader fileReader = new FileReader(file);
Reader reader = new InputStreamReader(new FileInputStream(file), "gbk");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
String jsonStr = sb.toString();
return jsonStr;
}
可行的代码
private String getText(String filePath) throws Exception {
InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "gbk"));
StringBuffer sb = new StringBuffer();
while(reader.ready()){
String line = reader.readLine();
sb.append(line);
}
reader.close();
inputStream.close();
String jsonStr = sb.toString();
return jsonStr;
}