本文介绍了FileInputStream.skip()是否进行搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将最后10MB的可能大文件复制到另一个文件中。理想情况下,我会使用FileInputStream,skip()然后read()。但是我不确定skip()的性能是否会很糟糕。 skip()通常是使用下面的文件搜索实现的,还是实际读取和丢弃数据?

I want to copy the last 10MB of a possibly large file into another file. Ideally I would use FileInputStream, skip() and then read(). However I'm unsure if the performance of skip() will be bad. Is skip() typically implemented using a file seek underneath or does it actually read and discard data?

我知道RandomAccessFile但是我对是否可以使用FileInputStream感兴趣代替那个(RandomAccessFile很烦人,因为API是非标准的)。

I know about RandomAccessFile but I'm interested in whether I could use FileInputStream in place of that (RandomAccessFile is annoying as the API is non-standard).

推荐答案

取决于你的JVM,但这里是最近的openjdk的 FileInputStream.skip()的来源:

Depends on your JVM, but here's the source for FileInputStream.skip() for a recent openjdk:

JNIEXPORT jlong JNICALL
Java_java_io_FileInputStream_skip(JNIEnv *env, jobject this, jlong toSkip) {
    jlong cur = jlong_zero;
    jlong end = jlong_zero;
    FD fd = GET_FD(this, fis_fd);
    if (fd == -1) {
        JNU_ThrowIOException (env, "Stream Closed");
        return 0;
    }
    if ((cur = IO_Lseek(fd, (jlong)0, (jint)SEEK_CUR)) == -1) {
        JNU_ThrowIOExceptionWithLastError(env, "Seek error");
    } else if ((end = IO_Lseek(fd, toSkip, (jint)SEEK_CUR)) == -1) {
        JNU_ThrowIOExceptionWithLastError(env, "Seek error");
    }
    return (end - cur);
}

看起来它正在做 seek()。但是,我不明白为什么 RandomAccessFile 是非标准的。它是 java.io 包的一部分,自1.0以来一直存在。

Looks like it's doing a seek(). However, I don't see why RandomAccessFile is non-standard. It's part of the java.io package and has been since 1.0.

这篇关于FileInputStream.skip()是否进行搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 11:34