本帖最后由 蔡汉康 于 2012-11-7 22:48 编辑
- import java.io.*;
- class EncodeStream
- {
- public static void main(String[] args) throws IOException
- {
- //writeText();
- readText();
- }
- public static void readText()throws IOException
- {
- InputStreamReader isr = new InputStreamReader(new FileInputStream("utf.txt"),"gbk");
- char[] buf = new char[10];//这里字符数组长度定义的问题不是很明白,为什么要设定成10,
- //写入用utf-8编码表(一个中文用3个字节表示), 读取时用gbk编码表(一个中文用2个字节表示)
- int len = isr.read(buf);//那么这个read()方法的在电脑上是怎么样运作的,能否解释一下过程,?????它不会读取10个字符吗????编译不会出错吗?????
- String str = new String(buf,0,len);
- System.out.println(str);
- isr.close();
- }
- public static void writeText()throws IOException
- {
- OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf.txt"),"UTF-8");
- osw.write("你好");
- osw.close();
- }
- }
复制代码 |