2

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 ...

Marquake
  • 191
  • 1
  • 3
  • 10

2 Answers2

5

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';
    }
}
Stuart Caie
  • 2,803
  • 14
  • 15
  • I understand the difference between read () and readFully () but if I have a file with 100 bytes ... the method readFully() I tell how many want to read and read () no, but the two will read 100 bytes. When I can have problems with the method read ()? – Marquake Mar 13 '14 at 11:39
  • The difference is that `read` only *promises* to read 1 byte. It might read 100, but it might also only read 1. It returns the number of bytes it did read, you have to check that it returned the number you expected. `readFully()` on the other hand promises to read all the bytes you ask for. – Stuart Caie Mar 14 '14 at 09:56
  • Perfect! That's my problem. Your class explains exactly what it does. I expected to see that code. I've even decompiled and have seen only this: // ---------      public int read () throws IOException {          fd.read return ();      } // ---------    int read () throws IOException {     return read (this.handle); } / / -------- private native int read (long paramLong) throws IOException; / / -------- but did not see what he did. Also, I have understood perfectly the difference between read () and readFully (). Thank you so much! – Marquake Mar 17 '14 at 16:28
  • @StuartCaie Is there a solution for getting the number of bytes read by `readFully`? It returns void.... – Kenny Worden Nov 10 '17 at 09:31
  • Simply use the length of the array you supplied to `readFully`. As its javadoc says, it blocks until exactly that many bytes have been read. If it can't read that many bytes, it throws `EOFException`. In that situation, you don't get to know how many bytes it read, only that there weren't enough to fill your array. – Stuart Caie Dec 06 '17 at 18:58
1
randomAccessFile.read (document) ;

This method will read the no. of bytes from the file, that is the length of your length of document array If length of document array is 1024 bytes, it will read 1024 bytes from file and place it in array.

Click here For Documentation of this method

and

document = randomAccessFile.read () ;

will just read one byte from file and return it, It will not read your whole file.

Click here for Documentation of this method

Mohammad Ashfaq
  • 1,333
  • 2
  • 14
  • 38
  • 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(); So that means RandomAccessFile does things that I can not see? just read, the content does not pass that line anywhere Thanks – Marquake Mar 13 '14 at 11:11