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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With