How the RandomAccessFile class returns bytes with randomAccessFile.read() method;

Marquake picture Marquake · Mar 13, 2014 · Viewed 8.7k times · Source

I'm using the google translator , I hope the question is well understood.

There is one thing I do not understand the random access files .No understand how the program works but it works.

This is my program :

// ---------------------------------------------
RandomAccessFile RandomAccessFile = new RandomAccessFile ( " pathfile ", " r");

byte [] document = new byte [ ( int) randomAccessFile.length ()] ;

randomAccessFile.read (document) ;
// ---------------------------------------------

In line 1 I access the file in read In line 2 I create a byte array object the same size as the file In line 3 reads the array of bytes

But never the file on the array of bytes is dumped .

I think the program should look something like :

/ / ---------------------------------------------
RandomAccessFile RandomAccessFile = new RandomAccessFile ( " pathfile ", " r");

byte [] document = new byte [ ( int) randomAccessFile.length ()] ;

// Line changed
document = randomAccessFile.read();
// ---------------------------------------------

The java documentation says :

randomAccessFile.read() ;

Reads a byte of data from this file . The byte is returned as an integer in
the range 0 to 255 ( 0x00- 0x0ff ) .

Only returns the number of bytes but not bytes.

Someone could explain to me how this line dumps the bytes in the byte [] variable document with this statement ?

randomAccessFile.read (document) ;

Thanks!!

// ------------------------------------------------------------------------------

Another example:

I compare this method with BufferedReader:

File file = new File ("C: \ \ file.txt"); 
FileReader fr = new FileReader (file); 
BufferedReader br = new BufferedReader (fr); 
... 
String line = br.readLine (); 

BufferedReader reads a line and passes it to a string.

I can see with this java statement that passes the file contents to a variable.

String line = br.readLine ();

But I do not see that with this other statement:

RandomAccessFile.read ();

just read, the content does not pass that line anywhere ...

Answer

Stuart Caie picture Stuart Caie · Mar 13, 2014

You should use readFully

    try (RandomAccessFile raf = new RandomAccessFile("filename", "r")) {
        byte[] document = new byte[(int) raf.length()];
        raf.readFully(document);
    }

Edit: you've clarified your question. You want to know why read does not "return" the contents of the file. How do the contents get there?

The answer is that read does not allocate any memory to store the contents of the file. You did that with new byte[length]. This is the memory where the file contents will go. You then call read and tell it to store the contents of the file in this array of bytes you have created.

BufferedReader.readLine does not operate like this because only it knows how many bytes need to be read for each line, so it does not make sense to get you to allocate them yourself.

Quick example of the "how":

class Test {
    public static void main(String args[]) {
        // here is where chars will be stored. If printed now, will show random junk
        char[] buffer = new char[5];

        // call our method. It does not "return" data.
        // It puts data into an array we already created.
        putCharsInMyBuffer(buffer);

        // prints "hello", even though hello was never "returned"
        System.out.println(buffer);
    }

    static void putCharsInMyBuffer(char[] buffer) {
        buffer[0] = 'h';
        buffer[1] = 'e';
        buffer[2] = 'l';
        buffer[3] = 'l';
        buffer[4] = 'o';
    }
}