skipBytes与DataInputStream的跳过之间的区别?

问题描述 投票:2回答:3

我现在想知道,这两种方法有什么区别:

[DataInputStream.skipBytesDataInputStream.skip

我知道skip必须来自InputStreamskipBytes来自DataInput的事实,但是仍然存在差异。您知道,在J2ME中使用流时,事情变得非常棘手,所以我需要知道!

从JSR-75的FileConnection返回的Input / DataInput流在处理方面是否会与其他任何此类流不同?

谢谢!

java java-me inputstream
3个回答
2
投票

来自DataInputStream

public final int skipBytes(int n) throws IOException {
    int total = 0;
    int cur = 0;

    while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
        total += cur;
    }

    return total;
    }

从代码中可以看到,skipBytes使用skip (InputStream.skip)

我唯一能说的是,如果包装的inputStream(DataInputStream中的InputStream)中的数据被另一个thread更改,那么skipBytes和skip的结果可能会不同。但是,如果您的应用程序仅使用单线程,则skipBytes和skip相同。


4
投票

另外,skip()作为参数要花费很长的时间,因此您可以一次跳过更多的字节。对大文件有用


0
投票

如果尝试使用。skip()返回而不是前进。您会注意到,它将成功返回字节数。但是,如果您尝试使用skipBytes()返回字节,假设您想向后移10个字节dis.skipBytes(n),它将无法正常工作,它将保持不变。因此,总而言之,这是skipskipBytes之间的主要区别。另一个区别是skip(long i)skipBytes(int i),skip采用long类型,这使您能够跳过较大文件的更多字节。

© www.soinside.com 2019 - 2024. All rights reserved.