`
geelong
  • 浏览: 115209 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

编码简介

阅读更多

1 gbk 支持简体和繁体中文  

gb2312 支持简体中文

 

2 utf 1-6直接变长   中英文混排比较省空间

 

取得本机编码

 

 

	public static void main(String[] args) {
		System.out.println(System.getProperty("file.encoding"));
//		UTF-8
//		ISO-8859-1    和在项目设置的Property编码相同
	}

 

 

成因及解决办法

 

 

package CharSetDemo02;

import java.io.BufferedWriter;
import java.io.OutputStream ;
import java.io.FileOutputStream ;
import java.io.File ;
import java.io.OutputStreamWriter;
public class CharSetDemo02{
	public static void main(String args[]) throws Exception {
		System.out.println(System.getProperty("file.encoding"));
//		本机编码 UTF-8
		
		File f = new File("D:" + File.separator + "test.txt") ;	// 实例化File类
		OutputStream out = new FileOutputStream(f) ;	// 实例化输出流
//		byte b[] = "中国,你好!".getBytes("ISO8859-1") ;	// 转码操作
//		乱码
		
//		byte b[] = "中国,你好!".getBytes("gbk") ;	// 转码操作
//		乱码
		
//		byte b[] = "中国,你好!".getBytes("UTF-8") ;	// 转码操作
//		按UTF-8来取系统默认编码的字节,如果系统不是UTF-8时,这会乱码。
//		比较好的做法是根据系统默认编码来取字节数组
//		byte b[] = "中国,你好!".getBytes(System.getProperty("file.encoding")) ;	// 转码操作

		
		//还可以
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
		writer.write("你好");
		writer.close();
		
		
//		out.write(b) ;	// 保存
//		out.close() ;	// 关闭
	}
};
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics