Reaching a specific line in a file using RandomAccessFile

Is it possible to place the cursor at the beginning of a specific line in a file through RandomAccessFile?

For example, I want to change the String from char 10 to 20 in line 111 in the file. The file has fixed length records.

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

Update:

I used the following code. However, his return is null.

The line length is 200 characters (this is 200 bytes, if I'm not mistaken)

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?

+6
source share
4 answers

I'm not sure, but it seems that RandomAccessFile does not support such functionality. Since RAF works with bytes, we can skip a certain number of bytes, and if your file has a fixed line width, this can be achieved with

 file.skipBytes(110 * lineSizeInBytes); 

Otherwise, you need something like this:

 for (int i = 0; i < 110; i++) file.readLine(); String line = file.readLine(); 
+3
source

You cannot do this directly with RandomAccessFile . It is designed to work with binary files and helps you read and write such fragments of files in any random place you want. This is why the class is called RandomAccessFile .

But it does not work with texts, so it has no way to recognize the end of a line and generally does not work in terms of lines.

So, in order to implement what you want, you must use BufferedReader , read line by line, and if you want to keep the position at which each line starts, so you can skip the required number of bytes to go to the beginning of the line you need.

+2
source

To use RandomAccessFile, you need to either have records of a fixed length or have a “doping vector” of offsets before the start of each record (or, for example, every 10th record). They may or may not correspond to your problem.

+1
source

According to some other people, there are other classes specifically designed to read lines of text, such as BufferedReader. However, if you need to use RandomAccessFile, you can read lines of text, but you need to programmatically find where 1 line ends and another line begins ...

A simple example would be ...

 RandomAccessFile raf = new RandomAccessFile("c:\test.txt","r"); String line = ""; while (raf.available()){ byte b = raf.read(); if (b == '\n'){ // this is the end of the current line, so prepare to read the next line System.out.println("Read line: " + line); line = ""; } else { line += (char)b; } } 

This gives the main building block for the reader who is looking for the end of each line.

If you intend to go the way of using RandomAccessFile, you can start with this structure, but you need to know about a few drawbacks and get-ya, such as ... 1. Unix and Windows use different line markers - you need to look for "\ n" , "\ r" and a combination of both of these 2. Reading one byte at a time is very slow - you have to read a block of bytes into the array buffer (for example, an array of bytes [2048]), and then iterate through the array, replenishing the array from RandomAccessFile when you reach the end of the buffer array. 3. If you are dealing with Unicode characters, you need to read and process 2 bytes at a time, not individual bytes.

RandomAccessFile is very powerful, but if you can use something like BufferedReader, then you will probably be much better off using it since it automatically takes care of all these issues.

+1
source

Source: https://habr.com/ru/post/911094/


All Articles