记录自己遇到的小问题,在eclipse中使用字符流复制txt文件乱码,代码如下:
package io;import java.io.*;public class CopyTextByBuf {public static void main(String[] args) {BufferedReader bufr =null;BufferedWriter bufw =null;try {bufr = new BufferedReader(new FileReader(“如何阅读一本书.txt”));bufw = new BufferedWriter(new FileWriter(“如何阅读一本.txt”));String line = null;while((line = bufr.readLine())!=null) {bufw.write(line);bufw.newLine();bufw.flush();}} catch (IOException e) {e.printStackTrace();}finally {if (bufw != null) {try {bufw.close();} catch (IOException e) {e.printStackTrace();}}if (bufr != null) {try {bufr.close();} catch (IOException e) {e.printStackTrace();}}}}}
乱码
这是由于本身的txt文件是utf-8,而我的这个文件被转为gbk编码的缘故。
解决:只需两步,进入window->preferences->General点开->按照图片设置即可
轻松解决: