0

I have a fixed format file.

I want to access specific lines in this file based on line numbers.

e.g. read line 100

The length of each line is 200 bytes.

So directly moving cursor to 100th line using RandomAccessFile would be like:

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

However, I am getting output as null.

What am I missing here ?

The question is in continuation to answer to my previous question Reaching a specific line in a file using RandomAccessFile

Update:

Below program works exactly as I am expecting:

Line size is 200 characters.

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

Giving linenumber (any line number from entire file) is printing that line.

@EJP: Please explain!

Community
  • 1
  • 1
Vicky
  • 16,679
  • 54
  • 139
  • 232
  • how you know the each line size is 100 byte. – Rakesh Patel Mar 20 '12 at 09:06
  • 1 character means 1 byte.. the line length is 200 char means 200 bytes... – Vicky Mar 20 '12 at 09:07
  • but suppose in single you are add multiple space,byte then it will add extra byte for that and this is bad programming. if you want to read line from 100 thenread the file using the readline from starting – Rakesh Patel Mar 20 '12 at 09:08
  • read this link http://stackoverflow.com/questions/2312756/in-java-how-to-read-from-a-file-a-specific-line-given-the-line-number or read this for example http://stackoverflow.com/questions/2138390/read-a-specific-line-from-a-text-file – Rakesh Patel Mar 20 '12 at 09:10
  • Your comments make sense when we dont know the size of the file.. here we do have that previous knowledge... and a space is also a byte... – Vicky Mar 20 '12 at 09:12
  • 1
    @downvoter: Please have courage to explain the downvoting! – Vicky Mar 20 '12 at 09:13
  • @NikunjChauhan I would also like to know. +1 to balance it, besides it being an interesting question that links back to the question that led to it. – Andrew Thompson Mar 20 '12 at 09:17
  • 1 character doesn't mean 1 byte at all, in Java. – user207421 Mar 20 '12 at 09:51
  • @EJP: The above program is working exactly as expected.... could you please throw some light explaining why its working and why its wrong ? – Vicky Mar 20 '12 at 09:57
  • If it's working 'exactly as specified' what exactly is your question about? – user207421 Mar 20 '12 at 09:59
  • @EJP: Please see my update.. the question NOW is why seek is working and skipBytes is not.... – Vicky Mar 20 '12 at 10:01

4 Answers4

1

RandomAccessFile.readLine() returns null at end of file. As it says in the Javadoc.

So you are at end of file.

So your calculations that say otherwise are incorrect.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

Firstly, if you are only going to be reading from the file, not writing to it, then I would suggest that you open the RandomAccessFile as "r" rather than "rw". The reason is that, if the file is open to allow write access, you can actually move the pointer in the file to a location that is larger than the length of the file, because you can potentially write the file as large as you want.

For example, if you have a file of 100 bytes in length, opening it in read-only mode will force you to keep the pointer somewhere in those 100 bytes. However, if you open the same file in read-write mode, you can tell RandomAccessFile to seek(250) and it will obey without any issues, as it thinks that you potentially want to write data out at this point in the file.

So, as some others have stated, its possible that you have moved past the end of the file, which would be quite valid in "rw" mode.

Secondly, if you are going to be reading from the beginning of the file, I would recommend that you use seek() rather than skipBytes(). By using seek(), you are guaranteeing that you are moving to the exact location you want to be, as it is always relative to the start of the file. However, skipBytes() is relative to the current pointer position in the file, so if you accidentally happened to move the pointer somewhere in your code between the RandomAccessFile constructor and the skipBytes() method call, you wouldn't arrive at the correct spot.

So, if you are going to be working from the beginning of the file, seek() provided a greater level of security that you are going to end up at the correct place in the file, no matter what else you do to the RandomAccessFile.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
0

What EJP said is correct: http://docs.oracle.com/javase/6/docs/api/java/io/RandomAccessFile.html

  • confirm that you have to correct file. Creating a RandomAccessFile with a non-existing file will throw an exception. Since you don't have the exception, you have created the RandomAccessFile with A file. Make sure it's the correct one.
  • The RandomAccessFile makes use of an internal file position pointer. If you're already at the end of the file, and skip some bytes again, it would explain the behavior that you mentioned. Please make sure you are at the correct position in the file.
Gijs Overvliet
  • 2,643
  • 3
  • 28
  • 35
  • @Gjis Overvliet: The code I have appended at the end of the question below UPDATE section.. is that correct ? I just want to confirm whether it will behave differently on different OS (unix, linux, windows, etc) ???? – Vicky Mar 20 '12 at 11:12
  • 1
    That code seems to be correct. There should not be any difference between different operating systems. – Gijs Overvliet Mar 20 '12 at 20:30
-1
byte[] line = new byte[200];
r.seek(200 * 99);
r.read(line);
String s = new String(line, "UTF-8"); // Or probably "Cp1252"
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138