3

I have a text file like

one
two
three
four
five

I need to get the offset of each line in the file. How do I do this in Java?

I have searched through some of the I/O libraries(like BufferedReader and RandomAccessFile) but I'm unable to find a satisfactory answer to this.

Can anyone suggest how to deal with this?

user3044327
  • 193
  • 1
  • 3
  • 11

2 Answers2

5

a) Byte offset 0, ie. file start
b) Open the file with something to read binary byte blocks (instead of strings etc.),
read the whole file (in a loop with something like up to 4096 byte each time)
and search the bytes with value '\n' in the block, in each loop iteration.
The position of each '\n' plus the count of previous block * 4096 is another line offset.

deviantfan
  • 11,268
  • 3
  • 32
  • 49
1

Another approach would be to count the bytes of each line line this

        BufferedReader br = null;   
    try {

        String line;
        // in my test each character was one byte
        ArrayList<Integer> byteoffset = new ArrayList<Integer>();

        br = new BufferedReader(new FileReader("numbers.txt"));
        Integer l = 0;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            Integer num_bytes = line.getBytes().length;
            System.out.println(num_bytes);
            byteoffset.add( l==0 ? num_bytes : byteoffset.get(l-1)+num_bytes );
            l++;
        }

    } catch ( Exception e) {

    }

in this example you would also need to add the size of the newline character \n to size of each line

clancer
  • 613
  • 4
  • 10
  • At least one possible problem: Different byte length for newline in text file mode depending on the OS (Windows \r\n, Linux \n ...) – deviantfan Mar 03 '14 at 00:41
  • Yes, but its nice that java provides a system line separator if you use this the length will be correct for whatever system you run on – clancer Mar 03 '14 at 01:01
  • No, because there is no guarantee that the file is using the current systems convention. (well, my thing is not entirely protable too, but for Windows, Linux, Mac, Unix... data it should work) – deviantfan Mar 03 '14 at 01:04