2

I was trying to read from a file between two specific indices using the RandomAccessFile.

I know I can jump to an index with the seek() function, but i couldn't find the answer how to read then text out of the file until a specific index.

For example i have a huge file and i want to read text from index 100 to index 500, i would start like this:

public String get_text(){
   raf.seek(100) //raf= my RandomAccessFile
   String txt=raf.read(500) //read until index 500 which i don't know how to do
   return txt;
}

Please help me :)

1 Answers1

-1

This is how I solved my Problem:

try {
    int index = 100;
    raf.seek(index); //index = 100
    int counter = 0;
    int length = 400;
    while (counter < length) { //want to read the characters 400 times
       char c = (char) raf.read();
       if (!(c == '\n')) {   //don't append the newline character to my result
           sb.append(c);    //sb is a StringBuilder
           counter++;
       }
    }
} catch (IOException e) {
    e.printStackTrace();
}

I saw also another solution, where readFully() was used with a bytes array, that also worked well.

try {
    raf.seek(index);
    byte[] bytes = raf.readFully(new byte[(int) length]); //length of the charactersequence to be read
    String str = bytes.toString();
} catch (IOException e){
    e.printStackTrace();
}

In this solution the length of the bytes array has to be considered within the newline character, so you have to calculate that considering the line length. ->Start in file with line breaks and end index in file with line breaks. length = endInFile-startInFile +1;

  • Second solution should use `readFully()`. Otherwise there is no guarantee the buffer was filled. – user207421 Mar 21 '19 at 23:47
  • hmm, i don't see that if I look at the JavaDocs https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html#readFully(byte[]) read() has more exception catches... can you explain why the buffer could not be filled? – Sophia Marie Mar 21 '19 at 23:58
  • Because there is nothing that says it *will* be filled in the contract of `read()`. That's why it returns a count. `readFully()` on the other hand is guaranteed to either fill the buffer or throw `EOFException` or another `IOException`, or block until one of these things happens. The `read()` method in your answer is neither of these, and is not provided, which makes your answer at best incomplete. – user207421 Mar 22 '19 at 09:20
  • @user207421 now I see, thank you, I will correct it. read() doesnt throw an EOFException but it throws IOExceptions: IOException - If the first byte cannot be read for any reason other than end of file, or if the random access file has been closed, or if some other I/O error occurs. – Sophia Marie Mar 22 '19 at 09:45
  • I don't know why you're telling me this. I know this. It is what I am telling you. It returns a count, *instead* of throwing `EOFException`. That is the point. Especially when you ignore the count. – user207421 Mar 22 '19 at 10:03
  • I'm sorry, I didn't want to upset you, I just wanted to understand what you mean. What I didn't mention is that I had an Index file for the file where I had to read out a RNA Sequence, therefore the length of the Buffer is culculated for the length of the RNA Sequence. In the file was the whole genomic RNA Sequence, if i had a length to read longer than the file is, i would have calculated something wrong, for my purpose it wasnt necessary to check for the end of the File. – Sophia Marie Mar 22 '19 at 11:37