标签:
问题的引入:在InputStreamReader(OutputStreamWriter)的构造方法中,有指定字符集编码,那么什么是字符集?有哪些常用的字符集?怎么用字符集进行编码?
一 什么是字符集?
字符:字符(Char)是各种文字和符号的总称,包括各国家文字、标点符号、图形符号、数字等。
字符集:字符集(Charset)是多个字符的集合,字符集种类较多,每个字符集包含的字符个数不同。
二 有哪些常用的字符集?
1.ASCII:
ASCII(American Standard Code for Information Interchange,美国信息互换标准编码)是基于罗马字母表的 一套电脑编码系统。
包含内容:
技术特征:
2.GB2312:
GB2312又称为GB2312-80字符集,全称为《信息交换用汉字编码字符集·基本集》,由原中国国家标准总局发布,1981年5月1日实施。
包含内容
字符集特点
字符集编码方法
字符集局限性

import java.io.*;
public class Test1 {
public static void main(String[] args) throws IOException {
try {
File file=new File("Test1.txt");
FileInputStream fis = new FileInputStream(file);
//这里就先不用Buffered缓冲区了
char []buf=new char[(int)file.length()];
//创建使用ASCII编码的InputStreamReader
InputStreamReader isr=new InputStreamReader(fis,"ASCII");
String text=isr.getEncoding();
System.out.println(text);
isr.read(buf);
FileOutputStream fos=new FileOutputStream(new File("haha.txt"));
//以ASCII编码的形式上写入
OutputStreamWriter osw=new OutputStreamWriter(fos,"ASCII");
osw.write(buf);
isr.close();
osw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
用上面代码写出的文件内容与原文件相同,只不过换了一种编码方式,如果在写的时候改成其他编码,则会出现乱码。
标签:
原文地址:http://www.cnblogs.com/Liang-Blog/p/5750329.html