DataInputStream 的skipBytes 和skip 之间的区别?

发布于 2024-09-19 11:05:32 字数 363 浏览 4 评论 0原文

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

DataInputStream.skipBytesDataInputStream.skip

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

从 JSR-75 中的 FileConnection 返回的输入/数据输入流在处理上与任何其他此类流有什么不同吗?

谢谢!

I wonder now, what is the difference between the two methods:

DataInputStream.skipBytes and DataInputStream.skip.

I am aware of the fact that skip must come from InputStream and skipBytes from DataInput, but still, is there any difference. You know, when using streams in J2ME, things get pretty tricky so I need to know!

Would the Input/DataInput Streams returned from the FileConnection in JSR-75 be any different in handling than any other such streams?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想你的星星会说话 2024-09-26 11:05:32

此外,skip() 采用 long 作为参数,因此您可以一次跳过更多字节。对于大文件很有用

Also, skip() takes a long as an argument, so you can skip over many more bytes at a time. Useful for large files

尘世孤行 2024-09-26 11:05:32

来自 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)中的数据被另一个线程更改,那么skipBytes和skip的结果可能会不同。但如果您的应用程序仅使用单线程,那么skipBytes 和skip 是相同的。

from 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;
    }

as you can see from the code, skipBytes uses skip (InputStream.skip)

the only thing that i can say is that if data in your wrapped inputStream (InputStream inside DataInputStream)changes by another thread, then the result of skipBytes and skip may be different. but if your application just working with single thread then skipBytes and skip are the same.

指尖上得阳光 2024-09-26 11:05:32

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

If you try to use .skip() to return back instead of moving forward. You will notice that it will successfully get back in bytes. However, if you try to go back in bytes using skipBytes(), let's say you want to move 10 bytes back dis.skipBytes(n), it won't work, it will stay the same. So, in conclusion this is the main difference between skip and skipBytes. Another difference is that, skip(long i), skipBytes(int i), skip takes a long type which gives you the ability to skip more bytes for larger files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文