我如何阅读最后的“ n” Java文件的字节数 [英] How do I read the last "n" bytes of a file in Java

查看:81
本文介绍了我如何阅读最后的“ n” Java文件的字节数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用RandomAccessFile的情况下读取文件的最后n个字节。

How do I read the last n number of bytes from a file, without using RandomAccessFile.

文件中的最后6个字节包含编写文件时的关键信息背部。我需要写原始文件,然后在其他地方附加最后6个字节。

The last 6 bytes in my files contain crucial information when writing the files back. I need to write my original files, and then append the last 6 bytes elsewhere.

任何指导?谢谢

推荐答案

您必须使用 RandomAccessFile
此类的实例支持读取和写入随机访问文件。随机访问文件的行为就像存储在文件系统中的大字节数组一样。

You have to do it by using RandomAccessFile.
Instances of this class support both reading and writing to a random access file. A random access file behaves like a large array of bytes stored in the file system.

RandomAccessFile randomAccessFile = new RandomAccessFile(your_file, "r");
randomAccessFile.seek(your_file.length() - n); 
randomAccessFile.read(byteArray, 0, n);

这篇关于我如何阅读最后的“ n” Java文件的字节数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆