Java - 无乱码读写文件
Java读取数据流的时候,一定要指定数据流的编码方式,否则将使用本地环境中的默认字符集。
BufferedReader reader = null;
String laststr = "";
try {
Reader in = new InputStreamReader(new FileInputStream(path), "UTF-8");
reader = new BufferedReader(in);
String tempString = null;
while ((tempString = reader.readLine()) != null) {
laststr = laststr + tempString;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return laststr;

浙公网安备 33010602011771号