IO流 关于字节流read()方法读取的问题

FooooDle 2016-09-12 05:30:41
private static void fun2(){
InputStream in=null;
try {
in=new FileInputStream("d:/干掉舒陪1.txt");
byte [] b=new byte[2];
int count=in.read(b);
while(count!=-1){
String s=new String(b,0,count);
System.out.print(s);
count=in.read(b);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
in=null;
}
}


如上代码,我想要读取上述干掉舒培的txt文件。直接运行读取内容成功。
然后我debug调试的时候出现了问题,在我走到创建数组b的时候,数组内容为0,这个时候是正常的。
但是当我走到下一步in=new FileInputStream("d:/干掉舒陪1.txt"); 的时候,走完了立马就出现了问题。
刚刚内容了0的数组b中出现了内容。 不是应该在我read读取的时候才出现吗?
因为这个问题,在debug调试完以后,我读取出来的内容是有丢失的。
然而实际直接运行的时候,程序并没有出现任何丢失和异常。 非常疑惑。求解??
下面是运行时的图:


...全文
643 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
FooooDle 2016-09-12
  • 打赏
  • 举报
回复
引用 6 楼 wlwlwlwl015 的回复:
[quote=引用 4 楼 FooooDle 的回复:] [quote=引用 1 楼 wlwlwlwl015 的回复:] 看一下InputStream的read方法的源码你就明白了:
public int read(byte b[], int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return 0;
        }

        int c = read();
        if (c == -1) {
            return -1;
        }
        b[off] = (byte)c;

        int i = 1;
        try {
            for (; i < len ; i++) {
                c = read();
                if (c == -1) {
                    break;
                }
                b[off + i] = (byte)c;
            }
        } catch (IOException ee) {
        }
        return i;
    }
是这样的,我在别人的电脑上运行是正常的。debug调试也没问题。但是自己电脑上的时候,debug调试在我new fileinputstream类的时候,他直接就给我数组里面值了。。似乎就像是提前缓存好了一样。。都没有到read方法的时候数组就有值了。这个也就是我奇怪的地方[/quote] 不过你的截图里你确实已经read过了啊?真有这种情况是不是你的流没有关闭导致的[/quote] 这里是截图的问题,,是在我read之前,new出来 Fileinputstream的时候,直接就给予了字节到数组里面。。 而且我尝试着重启了Myeclipse什么的都不行 = = 。。真的很费解。但是在别人的电脑上调试都没有问题
小灯光环 2016-09-12
  • 打赏
  • 举报
回复
引用 4 楼 FooooDle 的回复:
[quote=引用 1 楼 wlwlwlwl015 的回复:] 看一下InputStream的read方法的源码你就明白了:
public int read(byte b[], int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return 0;
        }

        int c = read();
        if (c == -1) {
            return -1;
        }
        b[off] = (byte)c;

        int i = 1;
        try {
            for (; i < len ; i++) {
                c = read();
                if (c == -1) {
                    break;
                }
                b[off + i] = (byte)c;
            }
        } catch (IOException ee) {
        }
        return i;
    }
是这样的,我在别人的电脑上运行是正常的。debug调试也没问题。但是自己电脑上的时候,debug调试在我new fileinputstream类的时候,他直接就给我数组里面值了。。似乎就像是提前缓存好了一样。。都没有到read方法的时候数组就有值了。这个也就是我奇怪的地方[/quote] 不过你的截图里你确实已经read过了啊?真有这种情况是不是你的流没有关闭导致的
小灯光环 2016-09-12
  • 打赏
  • 举报
回复
主要是这一行b[off + i] = (byte)c; read方法内部就会遍历字节数组并给它赋值,你在while之前已经调用了read方法
FooooDle 2016-09-12
  • 打赏
  • 举报
回复
引用 1 楼 wlwlwlwl015 的回复:
看一下InputStream的read方法的源码你就明白了:
public int read(byte b[], int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return 0;
        }

        int c = read();
        if (c == -1) {
            return -1;
        }
        b[off] = (byte)c;

        int i = 1;
        try {
            for (; i < len ; i++) {
                c = read();
                if (c == -1) {
                    break;
                }
                b[off + i] = (byte)c;
            }
        } catch (IOException ee) {
        }
        return i;
    }
是这样的,我在别人的电脑上运行是正常的。debug调试也没问题。但是自己电脑上的时候,debug调试在我new fileinputstream类的时候,他直接就给我数组里面值了。。似乎就像是提前缓存好了一样。。都没有到read方法的时候数组就有值了。这个也就是我奇怪的地方
FooooDle 2016-09-12
  • 打赏
  • 举报
回复
引用 2 楼 abcdefghiijklmnopqrs 的回复:
测试过了并没有数据的丢失 int count = in.read(b);这行就是读取数据,执行这行以后有了数据才正常
是这样的,我在别人的电脑上运行是正常的。debug调试也没问题。但是自己电脑上的时候,debug调试在我new fileinputstream类的时候,他直接就给我数组里面值了。。似乎就像是提前缓存好了一样。。都没有到read方法的时候数组就有值了。这个也就是我奇怪的地方
  • 打赏
  • 举报
回复
测试过了并没有数据的丢失 int count = in.read(b);这行就是读取数据,执行这行以后有了数据才正常
小灯光环 2016-09-12
  • 打赏
  • 举报
回复
看一下InputStream的read方法的源码你就明白了:
public int read(byte b[], int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return 0;
        }

        int c = read();
        if (c == -1) {
            return -1;
        }
        b[off] = (byte)c;

        int i = 1;
        try {
            for (; i < len ; i++) {
                c = read();
                if (c == -1) {
                    break;
                }
                b[off + i] = (byte)c;
            }
        } catch (IOException ee) {
        }
        return i;
    }

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧