简体   繁体   English

使用RandomAccessFile到达文件中的特定行

[英]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? 是否可以通过RandomAccessFile将光标定位到文件中特定行的开头?

For eg I want to change String starting at char 10 till 20 in line 111 in a file. 例如,我想在文件中第111行的char 10到20之间更改String。 The file has fixed length records. 该文件具有固定长度的记录。

Is it possible to directly position the cursor to start of line 111 using RandomAccessFile ? 是否可以使用RandomAccessFile将光标直接定位到第111行的开头?

Update: 更新:

I used the following code. 我使用了以下代码。 However, its returning null. 但是,它返回null。

Line length is 200 characters (which is 200 bytes if I am not wrong) 行长度为200个字符(如果我没有错,则为200个字节)

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 ? 我哪里错了?

I'm not sure but seems RandomAccessFile do not support such functionality. 我不确定,但似乎RandomAccessFile不支持这样的功能。 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 由于RAF以字节操作,我们可以跳过特定的字节数,如果你的文件有固定的行宽,这可以通过实现

file.skipBytes(110 * lineSizeInBytes);

Otherwise, you need something like this: 否则,你需要这样的东西:

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

You cannot do this directly with RandomAccessFile . 您无法使用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 . 这就是该类被称为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. 因此,要实现您想要的功能,您应该使用BufferedReader ,逐行读取,如果您希望每行开始的存储位置,那么您将能够跳过所需的字节数以跳转到所需行的开头。

To use RandomAccessFile you either need to have fixed-length records or have a "dope vector" of offsets to the start of each record (or, eg, every 10th record). 要使用RandomAccessFile,您需要具有固定长度的记录或者具有到每个记录的开头(或者例如,每第10个记录)的偏移的“涂料向量”。 These may or may not be appropriate to your problem. 这些可能适合您的问题,也可能不适合。

As some other people have stated, there are other classes that are specifically designed to read lines of text, such as BufferedReader. 正如其他一些人所说,还有其他专门用于读取文本行的类,例如BufferedReader。 However, if you are required to use RandomAccessFile, you can read lines of text, but you need to programmatically find where 1 line ends and another line begins... 但是,如果您需要使用RandomAccessFile,则可以读取文本行,但是您需要以编程方式查找1行结束的位置和另一行开始...

A simple example may 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 basic building block for a reader that looks for the end of each line. 这为查找每行结尾的阅读器提供了基本构建块。

If you intend to go down the path of using RandomAccessFile, you can start with this framework, but you need to be aware of a number of drawbacks and got-ya's such as... 1. Unix and Windows use different line markers - you'll need to look for '\\n', '\\r' and a combination of both of these 2. Reading a single byte at a time is very slow - you should read a block of bytes into an array buffer (eg a byte[2048] array) and then iterate through the array, refilling the array from the RandomAccessFile when you reach the end of the buffer array. 如果你打算沿着使用RandomAccessFile的道路走下去,你可以从这个框架开始,但是你需要注意一些缺点并得到诸如......之类的东西...... 1. Unix和Windows使用不同的行标记 - 你我需要寻找'\\ n','\\ r'以及这两者的组合2.一次读取一个字节非常慢 - 你应该将一个字节块读入一个数组缓冲区(例如一个字节) [2048]数组)然后遍历数组,当到达缓冲区数组的末尾时,从RandomAccessFile重新填充数组。 3. If you are dealing with Unicode characters, you need to read and process 2 bytes at a time, rather than single bytes. 3.如果要处理Unicode字符,则需要一次读取和处理2个字节,而不是单个字节。

RandomAccessFile is very powerful, but if you can use something like BufferedReader then you'd probably be much better to use that instead, as it takes care of all these issues automatically. RandomAccessFile非常强大,但是如果你可以使用像BufferedReader这样的东西,那么你可能会更好地使用它,因为它会自动处理所有这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM