BufferedReader and PrintWriter to read and write Chinese problem

Recently read a text file with the BufferedReader, then read out the contents and then write to another file with a new PrintWriter.

Before this problem has not been found, it is that if there are Chinese text content, and the content is written in the read-out content will be garbled.

I thought about solutions.

First use BufferedReader when reading out what character set encoding settings:

// file path: filePath 
File File = new new File (filePath);
FileInputStream fin = new FileInputStream(file);
InputStreamReader in = new InputStreamReader(fin,"GBK");
BufferedReader br = new BufferedReader(in);

 Once set up, you do some reading operation, and then call PrintWriter write, of course, also set the same character set encoding:

File tempFile = new File(newPath);
PrintWriter t pw = null ;
FileOutputStream fo = new FileOutputStream(tmpFile);
OutputStreamWriter osw = new OutputStreamWriter(fo,"GBK");
tpw = new PrintWriter (Abou);

 

Reproduced in: https: //www.cnblogs.com/xinmomoyan/p/11002217.html

Guess you like

Origin blog.csdn.net/weixin_34163741/article/details/93316528