import java.io.*;public class TestFileInputStream {
public static void main(String[] args) {
int b = 0;
FileInputStream in = null; try{
in = new FileInputStream("E:\\Myjava\\io\\TestTransfrom2.java"); } catch(FileNotFoundException fe) {
System.out.println("没有找到相应的文件!");
System.exit(-1);
}
try{
long num = 0;
while((b=in.read()) != -1) {
System.out.print((char)in.read());
num ++;
}
in.close();
System.out.println();
System.out.println("共读取了:" + num + "个字节!");
} catch (IOException e) {
System.out.println("文件读取错误!");
System.exit(-1);
}
}
}输出:
pbi ls etrnfo2{
pbi ttcvi anSrn[ r){
nuSraRae u;
u e nuSraRae(
Sse.u.rn(ca)u.ed);\rnfo.x")
}
Sse.u.rnl(;
u.ls(;cdn()
ac FlNtonEcpinf){
f.rnSakrc(;
Sse.u.rnl(??????";
.rnSakrc(;
ty{
e ienuSra(E\Mjv\i\TsTasrm.aa);
wieotra( =-){
ytmotpitnotgtnoig);
}cth(Oxeto ){pitn"??????)
}pittcTae)
}
?
共读取了:499个字节!但是将System.out.print((char)in.read());改为System.out.print((char)b)后输出正常
我不知道是怎么回事,请帮忙解释解释,谢谢!
public static void main(String[] args) {
int b = 0;
FileInputStream in = null; try{
in = new FileInputStream("E:\\Myjava\\io\\TestTransfrom2.java"); } catch(FileNotFoundException fe) {
System.out.println("没有找到相应的文件!");
System.exit(-1);
}
try{
long num = 0;
while((b=in.read()) != -1) {
System.out.print((char)in.read());
num ++;
}
in.close();
System.out.println();
System.out.println("共读取了:" + num + "个字节!");
} catch (IOException e) {
System.out.println("文件读取错误!");
System.exit(-1);
}
}
}输出:
pbi ls etrnfo2{
pbi ttcvi anSrn[ r){
nuSraRae u;
u e nuSraRae(
Sse.u.rn(ca)u.ed);\rnfo.x")
}
Sse.u.rnl(;
u.ls(;cdn()
ac FlNtonEcpinf){
f.rnSakrc(;
Sse.u.rnl(??????";
.rnSakrc(;
ty{
e ienuSra(E\Mjv\i\TsTasrm.aa);
wieotra( =-){
ytmotpitnotgtnoig);
}cth(Oxeto ){pitn"??????)
}pittcTae)
}
?
共读取了:499个字节!但是将System.out.print((char)in.read());改为System.out.print((char)b)后输出正常
我不知道是怎么回事,请帮忙解释解释,谢谢!
解决方案 »
- 笔试题:有多少种情况会引发IllegalMonitorStateException抛出
- 初学java,自己写的计算器
- 有关Java操作word的问题(jacob)
- 疑难杂症!!!请教!!bad class file textfield.class class file has wrong version 50.0 should be 49.0
- 关于JTable问题
- 窗口关闭事件
- 我在一本书上看到,cloneable是java的(误)拼写,那么符合英文规则的拼法应该怎样呢?
- accept()!
- 一道选择题8
- 请大家给点意见,原来做java,即将改做数据仓库,那一个更好?
- jna误错哪个大虾帮帮忙
- Thingking In Java 里面一段按位运算符的意思??
System.out.print((char)in.read());
num ++;
}b=in.read();你读过一次,指针向下走一位,
(char)in.read(); 如果有这么个字符 "ab" 循环一次就打一个 b 出来
你在循环体里又读一次System.out.print((char)in.read()),相当于读了2个字符,而打印了第2个字符,第1个字符你没打印,你没发现你改之前和改之后正好读取的字节数差了一倍吗