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;

 

posted @ 2017-07-11 13:52  肉炒辣椒  阅读(260)  评论(0)    收藏  举报