Eclipse控制台中输入中文保存到文件中,字符编码转换测试

 

Eclipse:代码编码格式UTF-8;控制台编码格式GBK

对“你好”编码解码

 

 

 

Eclipse:代码编码格式UTF-8;控制台编码格式UTF-8

对“你好”编码解码

public class EncodeDemo {
	public static void main(String[] args) throws IOException{
		String s = "你好";
		byte[] b1 = s.getBytes("utf-8");
		System.out.println(Arrays.toString(b1));
		String s1 = new String(b1, "iso-8859-1");
		System.out.println(s1);
	}
}
 

 

-----------------------------------------------------------------------------------------

编码测试: win7中文操作系统,Eclipse工程编码是UTF-8,Console是UTF-8

从eclipse的console中输入中文“你好”,保存在硬盘中txt格式,用记事本打开结果如下:

 

 

 

结论:


in: 默认编码是UTF-8

out:默认编码是UTF-8  


要想记事本打开正常:设置out编码为GBK
注意:但是再读取到Eclipse控制台,正常显示出来,不需要设置编码为GBK。

 

public static void readIn() throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in,"UTF-8"));//设置控制台读取编码
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\a.txt"),"UTF-8"));//设置写入 txt编码
		String s = null;
		while ((s = br.readLine()) != null) {
			if (s.equals("over")) {
				break;
			}
			bw.write(s);
			bw.newLine();
		}
		bw.flush();
		br.close();
		bw.close();
	}