如何从Java中的某个偏移量读取文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9671126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:56:15  来源:igfitidea点击:

How to read a file from a certain offset in Java?

javafile-io

提问by Khashayar

Hey I'm trying to open a file and read just from an offset for a certain length! I read this topic: How to read a specific line using the specific line number from a file in Java?in there it said that it's not to possible read a certain line without reading the lines before, but I'm wondering about bytes!

嘿,我正在尝试打开一个文件并从一定长度的偏移量中读取!我阅读了这个主题: How to read a specific line using the specific line number from a file in Java? 在那里它说不可能在不阅读之前阅读某一行的情况下阅读某一行,但我想知道字节!

FileReader location = new FileReader(file);
BufferedReader inputFile = new BufferedReader(location);
// Read from bytes 1000 to 2000
// Something like this
inputFile.read(1000,2000);

Is it possible to read certain bytes from a known offset?

是否可以从已知偏移量读取某些字节?

采纳答案by Woot4Moo

RandomAccessFileexposes a function:

RandomAccessFile公开一个函数:

seek(long pos) 
          Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.