import java.io.*;public class TestEOF {
public static void main(String[] args)
throws IOException {
DataInputStream in =
new DataInputStream(
new BufferedInputStream(
new FileInputStream("TestEof.java")));
while(in.available() != 0)
System.out.print((char)in.readByte());
}
}
看程序的最后一行:System.out.print((char)in.readByte());
假如in中有两个byte代表一个char:"a" 按照上面程序开始只能到读第一个byte.然后就把这个byte转换为char. 得到的字符应该不能正确表达字符"a".但是这样运行的时候输出正常.
而如果把readByte()改为readChar().这样似乎合理. 因为它每次读两个byte,正好代表一个char.但是这样运行的时候输出的却是乱码.
请指教!!!!!
public static void main(String[] args)
throws IOException {
DataInputStream in =
new DataInputStream(
new BufferedInputStream(
new FileInputStream("TestEof.java")));
while(in.available() != 0)
System.out.print((char)in.readByte());
}
}
看程序的最后一行:System.out.print((char)in.readByte());
假如in中有两个byte代表一个char:"a" 按照上面程序开始只能到读第一个byte.然后就把这个byte转换为char. 得到的字符应该不能正确表达字符"a".但是这样运行的时候输出正常.
而如果把readByte()改为readChar().这样似乎合理. 因为它每次读两个byte,正好代表一个char.但是这样运行的时候输出的却是乱码.
请指教!!!!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货