Java.io.RandomAccessFile Class Method | Set 2
- 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 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 Beginner For Beginner" ); 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 \nBeginner For Beginner" ); 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 Beginner For Beginner 1 readLine() : Hello Beginner For Beginners 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.
Contact Us