
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - RandomAccessFile read((byte[] b,int off,int len) method
Description
The Java RandomAccessFile read((byte[] b,int off,int len) method reads up to len bytes of data from this file into an array of bytes.
read((byte[] b,int off,int len) method −
Reads up to len bytes from the file into the byte array b, starting at index off in the array.
Returns the number of bytes actually read, or -1 if end of file (EOF) is reached.
This is useful for reading a portion of a file directly into a part of an existing array.
Declaration
Following is the declaration for java.io.RandomAccessFile.read((byte[] b,int off,int len) method.
public int read((byte[] b,int off,int len)
Parameters
b − The buffer into which the data is read.
off − The start offset in array b at which the data is written.
len − The maximum number of bytes read.
Return Value
This method returns the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
Exception
IOException − If an I/O error occurs. Not thrown if end-of-file has been reached.
NullPointerException − If b is null.
IndexOutOfBoundsException − If off is negative, len is negative, or len is greater than b.length - off.
Example - Usage of RandomAccessFile read((byte[] b,int off,int len) method
The following example shows the usage of RandomAccessFile read((byte[] b,int off,int len) method.
RandomAccessFileDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.RandomAccessFile; public class RandomAccessFileDemo { public static void main(String[] args) { try { byte[] b1 = {1, 2, 3}; byte[] b2 = {1, 2, 3, 4, 5, 6, 7, 8}; // create a new RandomAccessFile with filename test RandomAccessFile raf = new RandomAccessFile("test.txt", "rw"); // write something in the file raf.writeUTF("Hello World"); // set the file pointer at 0 position raf.seek(0); // read 2 bytes, starting from 1 System.out.println("" + raf.read(b1, 1, 2)); // set the file pointer at 0 position raf.seek(0); // read 3 bytes, starting from 4rth System.out.println("" + raf.read(b2, 4, 3)); } catch (IOException ex) { ex.printStackTrace(); } } }
Output
Assuming we have a text file test.txt in current directory which has the following content. This file will be used as an input for our example program −
ABCDE
Let us compile and run the above program, this will produce the following result −
2 3
Example - Reading specific number of bytes into part of an array
The following example shows the usage of RandomAccessFile read((byte[] b,int off,int len) method.
RandomAccessFileDemo.java
package com.tutorialspoint; import java.io.RandomAccessFile; import java.io.IOException; public class RandomAccessFileDemo { public static void main(String[] args) { try (RandomAccessFile raf = new RandomAccessFile("example1.txt", "rw")) { raf.write("ABCDEFGHIJ".getBytes()); // 10 bytes written byte[] buffer = new byte[10]; raf.seek(0); int bytesRead = raf.read(buffer, 2, 5); // Read 5 bytes into buffer starting at index 2 System.out.println("Bytes read: " + bytesRead); System.out.print("Buffer content: "); for (byte b : buffer) { System.out.print((b == 0 ? '.' : (char) b) + " "); } } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Bytes read: 5 Buffer content: . . A B C D E . . .
Explanation
File content: "ABCDEFGHIJ"
Reads 5 bytes starting at the beginning of the file.
Stores those 5 bytes starting at index 2 in the buffer.
Other parts of the buffer remain as default (0, shown as .).
Example - Reading from a certain position in the file into a subset of a buffer
The following example shows the usage of RandomAccessFile read((byte[] b,int off,int len) method.
RandomAccessFileDemo.java
package com.tutorialspoint; import java.io.RandomAccessFile; import java.io.IOException; public class RandomAccessFileDemo { public static void main(String[] args) { try (RandomAccessFile raf = new RandomAccessFile("example2.txt", "rw")) { raf.write("1234567890".getBytes()); // Write 10 bytes byte[] buffer = new byte[8]; raf.seek(5); // Start reading from the 6th byte ('6') int bytesRead = raf.read(buffer, 3, 4); // Read 4 bytes into buffer starting at index 3 System.out.println("Bytes read: " + bytesRead); System.out.print("Buffer content: "); for (byte b : buffer) { System.out.print((b == 0 ? '.' : (char) b) + " "); } } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Bytes read: 4 Buffer content: . . . 6 7 8 9 .
Explanation
File content: "1234567890"
Seeks to position 5 → points to '6'.
Reads 4 bytes ('6', '7', '8', '9') and places them at buffer index 3-6.
Buffer indices 0-2 and 7 remain as default (0).