- 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("d:\\123\\utf.txt"),"UTF-8");
- char[] buf=new char[1024];
- int len=isr.read(buf);
- 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("d:\\123\\utf.txt"),"GBK");
- osw.write("你好");
- osw.close();
- }
- }
复制代码 ================================================================================================- 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("d:\\123\\utf.txt"),"GBK");
- char[] buf=new char[1024];
- int len=isr.read(buf);
- 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("d:\\123\\utf.txt"),"UTF-8");
- osw.write("你好");
- osw.close();
- }
- }
复制代码 UTF-8是三个字节,GBK是两个字节,为什么第一个程序打印出问号?没有可能是前三个字节恰好读成了一个汉字 后面一个字节是问号吗?
|
-
1.jpg
(38.64 KB, 下载次数: 8)
-
2.jpg
(12.77 KB, 下载次数: 10)
结果
|