Monday 14 November 2016

Working with RandomAccessFile



RandomAccessFileencapsulates a random-access file.
 It implements the interfaces DataInputand DataOutput, which define the basic I/O methods. It also implements the Closeable interface.
 RandomAccessFileis special because it supports positioning request-i.e. one can position file pointer within the file.
 It has two constructors:
RandomAccessFile(File fileObj, String access) throws FileNotFoundException
RandomAccessFile(String fileObj, String access) throws FileNotFoundException
In the first form, fileObj specifies the name of the file to open as a Fileobject.
In the second form, the name of the file is passed in filename.
In both cases, access determines what type of file access is permitted.
If it is “r”, then the file can be read, but not written.
If it is “rw”, then the file is opened in read-write mode.
If it is “rws”, then the file is opened for read-write operation and every change to the file’s data or metadata will be immediately written to the physical device.
If it is “rwd”, then the file is opened for read-write operation and every change to the file’s data will be immediately written to the physical device.
 The seek() methodis used to set the current position of the file pointer within the file:
void seek(long newPos) throwsIOException
newPos specifies the new position, in bytes, of the file pointer from the beginning of the file.
 It also includes method setLength():
voidsetLength(long len) throws IOException
This method sets the length of the invoking file to that specifiedby len. This method can be used to lengthen or shorten a file. If the file is lengthened, the added portion is undefined

No comments:
Write comments