Reaching a specific line in a file using RandomAccessFile

后端 未结 4 778
清歌不尽
清歌不尽 2021-01-12 13:31

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

4条回答
  •  抹茶落季
    2021-01-12 14:00

    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();
    

提交回复
热议问题