Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reaching a specific line in a file using RandomAccessFile

Is it possible to position cursor to the start of a specific line in a file through RandomAccessFile?

For e.g. I want to change String starting at char 10 till 20 in line 111 in a file. The file has fixed length records.

Is it possible to directly position the cursor to start of line 111 using RandomAccessFile ?

Update:

I used the following code. However, its returning null.

Line length is 200 characters (which is 200 bytes if I am not wrong)

File f = new File(myFile); 
RandomAccessFile r = new RandomAccessFile(f,"rw"); 
r.skipBytes(200 * 99);   // linesize * (lineNum - 1) 
System.out.println(r.readLine());

Where am I going wrong ?

like image 539
Vicky Avatar asked Mar 19 '12 12:03

Vicky


People also ask

How do you read a specific line of a file in Java?

Java supports several file-reading features. One such utility is reading a specific line in a file. We can do this by simply providing the desired line number; the stream will read the text at that location. The Files class can be used to read the n t h nth nth line of a file.

How do I read in RandomAccessFile?

While creating the instance of RandomAccessFile in java, we need to provide the mode to open the file. For example, to open the file for read only mode we have to use “r” and for read-write operations we have to use “rw”. Using file pointer, we can read or write data from random access file at any position.

Which method of RandomAccessFile class reads a line from the file and returns it as a?

readLine() method reads the next line of text from this file. This method successively reads bytes from the file, starting at the current file pointer, until it reaches a line terminator or the end of the file.

What does RandomAccessFile seek do?

seek(long pos) method sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length.


2 Answers

I'm not sure but seems RandomAccessFile do not support such functionality. As RAF operates with bytes we can skip specific amount of bytes, and if your file has fixed line width this can be achieved by

file.skipBytes(110 * lineSizeInBytes);

Otherwise, you need something like this:

for (int i = 0; i < 110; i++) file.readLine();
String line = file.readLine();
like image 117
mishadoff Avatar answered Sep 22 '22 07:09

mishadoff


You cannot do this directly with RandomAccessFile. It is attended to work with binary files and helps you to read and write such files fragment at any random location you want. This is why the class is called RandomAccessFile.

But it does no work with texts, so it does not have a way to recognize end of line and does not work in terms of lines at all.

So, to implement what you want you should use BufferedReader, read line-by-line and if you want store position where each line is started, so you will be able to skip required number of bytes to jump to the beginning of needed line.

like image 29
AlexR Avatar answered Sep 23 '22 07:09

AlexR