Java.io.RandomAccessFile Class Method | Set 2 Last Updated : 25 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 line of text from this file readUnsignedByte() : java.io.RandomAccessFile.readUnsignedByte() reads an unsigned 8 bit number from file, starts reading from the current File Pointer. Syntax : public final int readUnsignedByte() Parameters : ------ Return : reads an unsigned 8 bit number from file readUnsignedShort() : java.io.RandomAccessFile.readUnsignedShort() reads an unsigned 16 bit number from file, starts reading from the current File Pointer. Syntax : public final int readUnsignedShort() Parameters : ------- Return : reads an unsigned 16 bit number from file readUTF() : java.io.RandomAccessFile.readUTF() reads in a string from the file Syntax : public final String readUTF() Parameters : ------- Return : Unicode String seek(long pos) : java.io.RandomAccessFile.seek(long pos) sets File pointer position. Syntax : public void seek(long pos) Parameters : pos : start position from file, measured in bytes Return : -------- setLength(long len) : java.io.RandomAccessFile.setLength(long len) sets length of the file. Syntax : public void setLength(long len) Parameters : len : desired length of the file Return : ------- skipBytes(int n) :java.io.RandomAccessFile.skipBytes(int n) skip over n bytes, discarding the skipped bytes Syntax : public int skipBytes(int n) Parameters : n : no. of bytes to be skipped Return : no. of bytes skipped getChannel() : java.io.RandomAccessFile.getChannel() returns unique FileChannel object associated with file. Syntax : public final FileChannel getChannel() Parameters : ------ Return : returns unique FileChannel object java.io.RandomAccessFile.length() : returns length of the file. Syntax : public long length() Parameters : ---- Return : length of the file, measured in bytes getFilePointer() : java.io.RandomAccessFile.getFilePointer() return current offset in the file in bytes. Syntax : public long getFilePointer() Parameters : ---- Return : return current offset in the file in bytes getFD() : java.io.RandomAccessFile.getFD() returns file descriptor object with the stream. Syntax : public final FileDescriptor getFD() Parameters : ----------- Return : file descriptor object with the stream. close() : java.io.RandomAccessFile.close() closes random access file stream and releases source associated with the stream, if any. Syntax : public void close() Parameters : ------- Return : ------- Java // Java Program illustrating use of io.RandomAccessFile class methods // seek(), readLine(), readUTF(), readUnsignedByte(), readUnsignedShort(), // setLength(), length(), skipBytes(), getFilePointer(), getChannel(), // getFD(), close() import java.io.*; public class NewClass { public static void main(String[] args) { try { // Creating a new RandomAccessFile - "GEEK" RandomAccessFile geek = new RandomAccessFile("FILE.txt", "rw"); // Writing to file geek.writeUTF("Hello Geeks For Geeks"); geek.seek(0); // Use of readUTF() : System.out.println("Use of readUTF() : " + geek.readUTF()); //Use of seek() : geek.seek(0); // Use of readLine() : System.out.println("1 readLine() : " + geek.readLine()); geek.seek(0); geek.writeUTF("Hello \nGeeks For Geeks"); geek.seek(0); System.out.println("2 readLine() : " + geek.readLine()); geek.seek(3); // Use of readUnsignedByte() : System.out.println("Use of readUnsignedByte() : " + geek.readUnsignedByte()); geek.seek(4); // Use of readUnsignedShort() : System.out.println("Use of readUnsignedByte() : " + geek.readUnsignedShort()); // Use of setLength(): geek.setLength(78); // Use of length() : System.out.println("Use of setLength() : " + geek.length()); geek.seek(2); // Use of skipBytes() : System.out.println("Use of skipBytes() : " + geek.skipBytes(3)); // Use of getFilePointer() : System.out.println("Use of getFilePointer() : " + geek.getFilePointer()); // Use of getChannel() : System.out.println("Use of getChannel() : " + geek.getChannel()); // Use of getFD() : System.out.println("Use of getFD() : " + geek.getFD()); // Use of close() method : geek.close(); System.out.println("Stream Closed."); } catch (IOException ex) { System.out.println("Something went Wrong"); ex.printStackTrace(); } } } Output : Use of readUTF() : Hello Geeks For Geeks 1 readLine() : Hello Geeks For Geekss 2 readLine() : Hello Use of readUnsignedByte() : 101 Use of readUnsignedByte() : 27756 Use of setLength() : 78 Use of skipBytes() : 3 Use of getFilePointer() : 5 Use of getChannel() : sun.nio.ch.FileChannelImpl@15db9742 Use of getFD() : java.io.FileDescriptor@6d06d69c Stream Closed. Comment More infoAdvertise with us Next Article SecureRandom setSeed() method in Java with Examples M Mohit Gupta Improve Article Tags : Misc Java Java-I/O Practice Tags : JavaMisc 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 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 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 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 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 Random next() method in Java with Examples The next() method of Random class returns the next pseudorandom value from the random number generator's sequence. Syntax: protected int next(int bits) Parameters: The function accepts a single parameter bits which are the random bits. Return Value: This method returns the next pseudorandom number. 1 min read Like