Java字符编码
一、字符编码:
列出系统参数:
public class EncodeDemo {
public static void main(String[] args) { System.getProperties().list(System.out);
}
}
二、对字符串指定编码:
范例:
import java.io.UnsupportedEncodingException;
public class EncodeDemo2 {
public static void main(String[] args) throws UnsupportedEncodingException {
String s="郭旭169512";
byte[] bs=s.getBytes("ISO8859-1");
System.out.println(new String(bs));
}
}
三、修改默认编码:
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class EncodeDemo {
public static void main(String[] args) throws Exception {
System.getProperties().put("file.encoding", "gb2312");
String s="中国人民万岁,haha";
byte[] b=s.getBytes("iso8859-1");
OutputStream out=new FileOutputStream(new File("c:/hello.txt"));
out.write(b);
out.close();
}
}