FileInputStream fi = new FileInputStream("c:/cmd.txt");
while((temp=fi.read())!=-1)
System.out.print((char)temp);InputStreamReader ir = new InputStreamReader((new FileInputStream("c:/cmd.txt")));
int temp;
System.out.print(ir.getEncoding());
while((temp=ir.read())!=-1)
System.out.print((char)temp);cmd.txt里面是英文文本既然有字节流和字符流的区别
字节流是以一个字节为单位,而字符流是2个字节为单位的
为什么这两段程序的read输出都一样?
下面的不是要每次读出2个字节的内容吗?
————————————————————
网上摘来的 老实说我不是很明白其中的具体编码细节,只知道能这样用,知道的讲下,谢谢!!!
while((temp=fi.read())!=-1)
System.out.print((char)temp);InputStreamReader ir = new InputStreamReader((new FileInputStream("c:/cmd.txt")));
int temp;
System.out.print(ir.getEncoding());
while((temp=ir.read())!=-1)
System.out.print((char)temp);cmd.txt里面是英文文本既然有字节流和字符流的区别
字节流是以一个字节为单位,而字符流是2个字节为单位的
为什么这两段程序的read输出都一样?
下面的不是要每次读出2个字节的内容吗?
————————————————————
网上摘来的 老实说我不是很明白其中的具体编码细节,只知道能这样用,知道的讲下,谢谢!!!
对于UTF-8,英文是1个,中文是3个
对于Unicode,英文中文都是2个
System.out.print(ir.getEncoding());猛一看还以为InputStreamReader 可以动态识别字符编码,
试了一下,发现ir.getEncoding()永远是GBK,和cmd.txt文件本身的编码没关系如果cmd.txt保存成UTF-8,这段程序就是乱码
........
————————————————————
靠 有这种事,我试下看看。
这也说明记事本不止根据文件头的字符判断。用记事本保存的UTF-8文件文件头是:EF BB BF手工把文件头的这3个字节删除,记事本还能识别成UTF-8,而不会误认为是GBK