Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

file-io

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!

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?

like image 621
Khashayar Avatar asked Mar 12 '12 16:03

Khashayar


People also ask

How do I read a specific text file in Java?

There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader, or Scanner to read a text file. Every utility provides something special e.g. BufferedReader provides buffering of data for fast reading, and Scanner provides parsing ability.

How read data from line from file in Java?

Java Read File line by line using BufferedReader We can use java. io. BufferedReader readLine() method to read file line by line to String. This method returns null when end of file is reached.

How do you read the contents of a file into a string in Java?

The readString() method of File Class in Java is used to read contents to the specified file. Return Value: This method returns the content of the file in String format. Note: File. readString() method was introduced in Java 11 and this method is used to read a file's content into String.

How do you display the contents of a file in Java?

To read the contents of a file into a Java application, we can create a FileInputStream to the file and call its read() method to read its bytes one at a time. An OutputStream outputs a stream of bytes from a Java application. System. out is a PrintStream, which is a type of OutputStream.


1 Answers

RandomAccessFile exposes a function:

seek(long pos)            Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. 
like image 185
Woot4Moo Avatar answered Sep 24 '22 03:09

Woot4Moo