RandomAccessFile read(byte[]) in Java

read(byte[]): This method is available in the java.io.RandomAccessFile class of Java.

Syntax:

int java.io.RandomAccessFile.read(byte[] b) throws IOException

This method takes one argument. This method reads up to b.length bytes of data from this file into an array of bytes. This method blocks until at least one byte of input is available.

Parameters: One parameter is required for this method.

b: the buffer into which the data is read.

Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of this file has been reached.

Throws:

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

2. NullPointerException - If b is null.

Approach 1: When no exception

Java

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileread2 {
    public static void main(String[] args) throws IOException {

        File file = new File("D:\\hello.txt");
        String mode = "r";
        RandomAccessFile randomAccessFile =
new RandomAccessFile(file, mode);

        byte b[] = { 'a', 'b', 'c' };

        System.out.println(randomAccessFile.read(b));
        randomAccessFile.close();
    }
}

Output:

-1


Approach 2: IOException

Java

package com.RandomAccessFile;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileread2 {
    public static void main(String[] args) throws IOException {

        File file = new File("D:\\hello.txt");
        String mode = "r";
        RandomAccessFile randomAccessFile =
new RandomAccessFile(file, mode);

        byte b[] = { 'a', 'b', 'c' };

        randomAccessFile.close();
        System.out.println(randomAccessFile.read(b));

    }
}

Output:

Exception in thread "main" java.io.IOException: Stream Closed at java.base/java.io.RandomAccessFile.readBytes(Native Method) at java.base/java.io.RandomAccessFile.read(RandomAccessFile.java:431) at com.RandomAccessFile.RandomAccessFileread2.main(RandomAccessFileread2.java:17)


Approach 3: NullPointerException 

Java

package com.RandomAccessFile;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileread2 {
    public static void main(String[] args) throws IOException {

        File file = new File("D:\\hello.txt");
        String mode = "r";
        RandomAccessFile randomAccessFile =
new RandomAccessFile(file, mode);

        byte b[] = null;

        System.out.println(randomAccessFile.read(b));
        randomAccessFile.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "b" is null at java.base/java.io.RandomAccessFile.read(RandomAccessFile.java:431) at com.RandomAccessFile.RandomAccessFileread2.main(RandomAccessFileread2.java:16)


Some other methods of RandomAccessFile

close()This method closes this random access file stream and releases any system resources associated with the stream.

getChannel()This method returns the unique FileChannel object associated with this file.

getFD()This method returns the opaque file descriptor object associated with this stream.

getFilePointer()This method returns the current offset in this file.

length()This method returns the length of this file.

RandomAccessFile(File, String)This method creates a random access file stream to read from, and optionally to write to, the file specified by the File argument. 

RandomAccessFile(String, String)This method creates a random access file stream to read from, and optionally to write to, a file with the specified name.

read()This method reads a byte of data from this file.

read(byte[])This method reads up to b.length bytes of data from this file into an array of bytes.

read(byte[], int, int)This method reads up to len bytes of data from this file into an array of bytes.

readBoolean()This method reads a single byte from the file, starting at the current file pointer.

readByte()This method reads a signed eight-bit value from this file.

readChar()This method reads a character from this file. This method reads two bytes from the file, starting at the current file pointer.

readDouble() This method reads a long value, starting at the current file pointer, as if by the readLong method, and then converts that long to a double using the longBitsToDouble method in class Double.

readFloat()This method reads an int value, starting at the current file pointer, as if by the readInt method, and then converts that int to a float using the intBitsToFloat method in class Float.

readFully(byte[])This method reads b.length bytes from this file into the byte array, starting at the current file pointer. 

readFully(byte[], int, int)This method reads exactly the len bytes from this file into the byte array, starting at the current file pointer.

readInt()This method reads a signed 32-bit integer from this file. This method reads 4 bytes from the file, starting at the current file pointer.

readLine()This method reads the next line of text from this file.

readLong()This method reads a signed 64-bit integer from this file. This method reads eight bytes from the file, starting at the current file pointer.

readShort()This method reads a signed 16-bit number from this file. The method reads two bytes from this file, starting at the current file pointer.

readUnsignedByte()This method reads an unsigned eight-bit number from this file. This method reads a byte from this file, starting at the current file pointer, and returns that byte.

readUnsignedShort()This method reads an unsigned 16-bit number from this file. This method reads two bytes from the file, starting at the current file pointer.

readUTF()This method reads in a string from this file. The string has been encoded using a modified UTF-8 format.

seek(long)This method sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.

setLength(long)This method sets the length of this file.

skipBytes(int)This method attempts to skip over n bytes of input discarding the skipped bytes.

write(byte[])This method writes b.length bytes from the specified byte array to this file, starting at the current file pointer.

write(int)This method writes the specified byte to this file.

write(byte[], int, int)This method writes len bytes from the specified byte array starting at offset off to this file.

writeBoolean(boolean)This method writes a boolean to the file as a one-byte value.

writeByte(int)This method writes a byte to the file as a one-byte value.

writeBytes(String)This method writes the string to the file as a sequence of bytes.

writeChar(int v)This method writes a char to the file as a two-byte value, high byte first.

writeChars(String)This method writes a string to the file as a sequence of characters.

writeDouble(double)This method converts the double argument to a long using the doubleToLongBits method in class Double and then writes that long value to the file as an eight-byte quantity, high byte first.

writeFloat(float)This method converts the float argument to an int using the floatToIntBits method in class Float and then writes that int value to the file as a four-byte quantity, high byte first.

writeInt(int)This method writes an int to the file as four bytes, the high byte first.

writeLong(long)This method writes a long to the file as eight bytes, high byte first.

writeShort(int)This method writes a short to the file as two bytes, the high byte first.

writeUTF(String)This method writes a string to the file using modified UTF-8 encoding in a machine-independent manner.

No comments:

Post a Comment