(JAVA) 从文件读取 "buffer" 字节(或 "y" 字节 if (y < buffer))

(JAVA) Read "buffer" bytes (or "y" bytes if (y < buffer)) from a file

我正在尝试从文件 "A" 中读取字节。我正在使用 RandomAccessFile 寻找位置,然后我需要读取 "y" 字节。

我有一个 4096 字节的缓冲区,如果 "y" 不是 4096 的倍数,我读取的字节数比我应该读取的多。

如果我将缓冲区设置为1字节,我可以毫无问题地读写(但是,当然,它太慢了)。

我现在的密码是:

public void extractFile(int pos) {
    try {
        RandomAccessFile raf = new RandomAccessFile(this.archive, "r");

        /* Trying to read */
        raf.seek(arquivos.get(pos).getPosicaoInicio());
        ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
        byte[] buf = new byte[1]; // With 1 I can read, because every "y" is multiple of 1
        byte[] bytes;
        while (byteOutput.size() < arquivos.get(pos).getTamanho()) {
            byteOutput.write(buf, 0, raf.read(buf));
        } 
        bytes = byteOutput.toByteArray();
        byteOutput.close();
        raf.close();

        /* Writing */
        File futuroArquivo = new File(arquivos.get(pos).getNome());
        FileOutputStream fos = new FileOutputStream(futuroArquivo);
        fos.write(bytes);
        fos.flush();
        fos.close();

    } catch (IOException ex) {

    }
}

PS: "arquivos.get(pos).getTamanho()" 是我的 "y"

PS 2:我无法读取整个文件,因为在"y"字节之后,还有其他东西

缓冲区可以是零以上的任何大小,ByteArrayOutputStream 简直就是浪费时间。并且 space。您假设 read() 也填充了缓冲区。更好的写法是:

RandomAccessFile raf = new RandomAccessFile(this.archive, "r");

/* Trying to read */
raf.seek(arquivos.get(pos).getPosicaoInicio());
byte[] buf = new byte[8192]; // or more, whatever you like really

/* Writing */
File futuroArquivo = new File(arquivos.get(pos).getNome());
FileOutputStream fos = new FileOutputStream(futuroArquivo);
int count;
long rest = arquivos.get(pos).getTamanho();
while (rest > 0 && (count = raf.read(buf, 0, (int)Math.min(buf.length, rest))) > 0)
{
    fos.write(buf, 0, count);
    rest -= count;
}
fos.close();
raf.close();

我也会考虑在 FileInputStream 周围使用 BufferedInputStream,而不是 RandomAccessFile。您并没有真正进行随机访问,只是初始搜索或跳过。