【IO流重点】转换流可以修改编码格式,FileWriter和FileReader的默认编码格式是gbk

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class Test2 {

	/*
	 * 转换流可以修改编码格式,FileWriter和FileReader的默认编码格式是gbk
	 */
	public static void main(String[] args) throws IOException {
		BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
//		BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("c:\\jingtianxiaozhi.txt"));
		BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\jingtianxiaozhi.txt"),"utf-8"));
		String line=null;
		while((line=bufferedReader.readLine())!=null){
			bufferedWriter.write(line.toUpperCase());
			bufferedWriter.newLine();
			bufferedWriter.flush();
		}
		bufferedReader.close();
		bufferedWriter.close();
	}
}

FileOutputStream的编码格式是:utf-8

用FileReader(gbk)去读,会出现乱码

 

 

 【IO流重点】转换流可以修改编码格式,FileWriter和FileReader的默认编码格式是gbk_第1张图片


你可能感兴趣的:(闲着没事—玩玩Java)