JAVA 乱码

1. Tomcat默认的字符编码是"ISO-8859-1"。

2. Tomcat更改编码集,在server.xml的配置文件中更改。

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>

3. JAVA如何将乱码字符串显示正常,首先要知道乱码的编码集,获取乱码字符串的byte[],再转换成你要的编码。

new String(oriStr.getBytes("original charset"),"new charset");

4. 如果JAVA文件是GBK编码,那在new String对象时,会按照GBK来编码,但如果外部文件的编码是UTF8,直接读取外部文件new String对象就会乱码,必须在new String对象时,指定其原来的编码格式-UTF8,生成的String对象按GBK编码来显示就不会出现乱码。

public static void main(String[] args){
	File file = new File("F:/chinese.txt");
	FileInputStream fis;
	try {
		fis = new FileInputStream(file);
		byte[] bytes = new byte[1024];
		String str = "";
		int count;
		while ((count=fis.read(bytes)) != -1) {
			str = str + new String(bytes, 0, count,"utf8");//text file is utf8, java file is GBK
			//str = str + new String(bytes, 0, count);//error
		}
		System.out.println(str);
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

posted on 2011-12-29 21:44  elm  阅读(325)  评论(0编辑  收藏  举报

导航