Java.io.RandomAccessFile Class Method | Set 3 Last Updated : 13 Oct, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Set 1, Set 2 More Methods of io.RandomAccessFile Class : write(int bytes) : java.io.RandomAccessFile.write(int bytes) writes specified byte to the file, starting from the current file pointer. Syntax : public void write(int bytes) Parameters : bytes : bytes to be written Return : ----- Exception : IOException :in case an I/O error occurs. write(byte[] b) : java.io.RandomAccessFile.write(byte[] b) writes b.length bytes from the specific byte array to the file, starting from the current file pointer. Syntax : public void write(byte[] b) Parameters : b : data to be written Return : ------- Exception : IOException :in case an I/O error occurs. write(byte[] b, int offset, int len) : java.io.RandomAccessFile.write(byte[] b, int offset, int len) writes bytes initialising from offset position upto b.length from the buffer. Syntax : public int write(byte[] b, int offset, int len) Parameters : b : buffer to write offset : starting position to write len : max no of bytes to write Return : --------- Exception : IOException :in case an I/O error occurs. writeBoolean(boolean b) : java.io.RandomAccessFile.writeBoolean(boolean b) writes a boolean to file as one-byte value. True is written is value is '1' else false Syntax : public final void writeBoolean(boolean b) Parameters : b : boolean value to be written Return : --------- Exception : IOException :in case an I/O error occurs. writeByte(int b) : java.io.RandomAccessFile.writeByte(int b) writes a byte to file as one-byte value, starting from the current position. Syntax : public final void writeByte(int b) Parameters : b : byte to be written Return : --------- Exception : IOException :in case an I/O error occurs. writeShort(int b) : java.io.RandomAccessFile.writeShort(int b) writes a short to file as two-byte value, starting from the current position. Syntax : public final void writeShort(int b) Parameters : b : short to be written Return : --------- Exception : IOException :in case an I/O error occurs. writeChar(int c) : java.io.RandomAccessFile.writeChar(int c) writes a char to file as two-byte value, starting from the current position. Syntax : public final void writeChar(int c) Parameters : c : char to be written Return : --------- Exception : IOException :in case an I/O error occurs. writeInt(int i) : java.io.RandomAccessFile.writeInt(int i) writes a int to file as four-byte value, starting from the current position. Syntax : public final void writeInt(int i) Parameters : i : int value to be written Return : --------- Exception : IOException :in case an I/O error occurs. writeLong(long l) : java.io.RandomAccessFile.writeLong(long l) writes a int to file as eight-byte value, starting from the current position. Syntax : public final void writeLong(long l) Parameters : l : long value to be written Return : --------- Exception : IOException :in case an I/O error occurs. writeFloat(float f) : java.io.RandomAccessFile.writeFloat(float f) converts the float argument to int and then writes a int to file as four-byte value, starting from the current position. Syntax : public final void writeFloat(float f) Parameters : f : float value to be written Return : --------- Exception : IOException :in case an I/O error occurs. writeDouble(double d) : java.io.RandomAccessFile.writeDouble(double d) converts the double argument to long and then writes a int to file as 8-byte value, starting from the current position. Syntax : public final void writeDouble(double d) Parameters : d : double value to be written Return : --------- Exception : IOException :in case an I/O error occurs. writeBytes(String s) : java.io.RandomAccessFile.writeBytes(String s) writes string to file as a sequence of bytes. Syntax : public final void writeBytes(String s) Parameters : s : byte string to be written Return : --------- Exception : IOException :in case an I/O error occurs. writeUTF(String str) : java.io.RandomAccessFile.writeUTF(String str) writes a string to file using modified UTF-8 encoding(machine-independent) Syntax : public final void writeUTF(String str) Parameters : str : String to be written Return : --------- Exception : IOException :in case an I/O error occurs. Java // Java Program illustrating use of io.RandomAccessFile class methods // writeUTF(), writeChar(), writeDouble(), writeFloat(), write(byte[] b), // write(int i), writeBoolean(), writeLong(), writeShort() // write(byte[] b, int offset, int len) import java.io.*; public class NewClass { public static void main(String[] args) { try { long l = 458754545576l; double d = 1.5; float f = 14.56f; int i = 1; boolean bol = true; short s = 15; // Creating a new RandomAccessFile - "GEEK" RandomAccessFile geek = new RandomAccessFile("GEEK.txt", "rw"); // writeUTF() : geek.writeUTF("Hello Geeks For Geeks"); geek.seek(0); System.out.println("writeUTF : " + geek.readUTF()); geek.seek(0); byte[] b = {1, 2, 3, 6, 5, 4}; // write(byte[] b) : geek.write(b); geek.seek(0); System.out.println("Use of .read(byte[] b) : " + geek.read(b)); // write(int i) : geek.write(i); geek.seek(0); System.out.println("write(int i) : " + geek.read(b)); // writeBoolean() : geek.writeBoolean(bol); geek.seek(0); System.out.println("writeBoolean() : " + geek.readBoolean()); geek.write(b, 2, 2); geek.seek(0); System.out.println("write(byte[] b, int offset, int len) : " + geek.readByte()); // writeChar() : geek.writeChar('c'); geek.seek(0); System.out.println("writeChar() : " + geek.readChar()); geek.seek(0); // writeDouble() : geek.writeDouble(d); geek.seek(0); System.out.println("writeDouble() : " + geek.readDouble()); geek.seek(0); //writeFloat() : geek.writeFloat(f); geek.seek(0); System.out.println("writeFloat() : " + geek.readFloat()); // writeLong() : geek.writeLong(l); geek.seek(0); System.out.println("writeLong() : " + geek.readLong()); // writeShort() : geek.writeShort(s); geek.seek(0); System.out.println("writeShort() : " + geek.readShort()); } catch (IOException ex) { System.out.println("Something went Wrong"); ex.printStackTrace(); } } } Output : writeUTF : Hello Geeks For Geeks Use of .read(byte[] b) : 6 write(int i) : 6 writeBoolean() : true write(byte[] b, int offset, int len) : 1 writeChar() : c writeDouble() : 1.5 writeFloat() : 14.56 writeLong() : 4713287227910652010 writeShort() : 16744 Comment More infoAdvertise with us Next Article Java.io.RandomAccessFile Class Method | Set 2 M Mohit Gupta Improve Article Tags : Java Java-I/O java-file-handling Practice Tags : Java Similar Reads Java.io.RandomAccessFile Class Method | Set 1 Java.io.RandomAccessFile Class provides a way to random access files using reading and writing operations. It works like an array of byte storted in the File. Declaration : public class RandomAccessFile extends Object implements DataOutput, DataInput, Closeable Methods of RandomAccessFile Class : re 5 min read Java.io.RandomAccessFile Class Method | Set 2 Set 1, Set 3 Methods of Java.io.RandomAccessFile Class Method : readLine() : java.io.RandomAccessFile.readLine() reads the next line of text from this file, start reading from the File Pointer till the end of file. Syntax : public final String readLine() Parameters : ----- Return : reads the next li 4 min read Random setSeed() method in Java with Examples The setSeed() method of Random class sets the seed of the random number generator using a single long seed. Syntax: public void setSeed() Parameters: The function accepts a single parameter seed which is the initial seed. Return Value: This method has no return value. Exception: The function does no 2 min read SecureRandom setSeed() method in Java with Examples setSeed( byte[] seed ) The setSeed() method of java.security.SecureRandom class is used to reseeds this random object. The given seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness. Syntax: public void setSeed(byte[] seed) Paramet 4 min read StrictMath random() Method in Java The random() is an inbuilt method of StrictMath class in java which is used to get a double value with a positive sign that is greater than or equal to 0.0 and less than 1.0. random() method is accurately organized to acquiesce appropriate use by more than one thread. The values which are returned a 2 min read Java.util.Random class in Java Random class is used to generate pseudo-random numbers in java. An instance of this class is thread-safe. The instance of this class is however cryptographically insecure. This class provides various method calls to generate different random data types such as float, double, int. Constructors: Rando 4 min read Like