20.3.RandomAccessFile
Sunday, April 18, 2010
In this post, first we need to look at the Class hierarchy of Input and OutputStreams in java.io.
Just see the fallowing diagram to understand the Class hierarchy.
RandomAccessFile
RandomAccessFile is used for files containing records of known size so that you can move from one record to another using seek( ), then read or change the records. The records don’t have to be the same size; you just have to be able to determine how big they are and where they are placed in the file.
At first it’s a little bit hard to believe that RandomAccessFile is not part of the InputStream or OutputStream hierarchy. It has no association with those hierarchies other than that it happens to implement the DataInput and DataOutput interfaces (which are also implemented by DataInputStream and DataOutputStream).
It doesn’t even use any of the functionality of the existing InputStream or OutputStream classes – it’s a completely separate class, written from scratch, with all of its own (mostly native) methods. The reason for this may be that RandomAccessFile has essentially different behavior than the other IO types, since you can move forward and backward within a file.
Essentially, a RandomAccessFile works like a DataInputStream pasted together with a DataOutputStream and the methods getFilePointer( ) to find out where you are in the file, seek( ) to move to a new point in the file, and length( ) to determine the maximum size ofthe file.
In addition, the constructors require a second argument (identical to fopen( ) in C)indicating whether you are just randomly reading (“r”) or reading and writing (“rw”).
There’s no support for write-only files, which could suggest that RandomAccessFile might have worked well if it were inherited from DataInputStream.
The seeking methods are available only in RandomAccessFile, which works for files only.
Let’s have a look at how the RandomAccessFile will work by the help of an example.
That's all for this time folks !, we will meet once again with some interesting topics.
Just see the fallowing diagram to understand the Class hierarchy.
RandomAccessFile
RandomAccessFile is used for files containing records of known size so that you can move from one record to another using seek( ), then read or change the records. The records don’t have to be the same size; you just have to be able to determine how big they are and where they are placed in the file.
At first it’s a little bit hard to believe that RandomAccessFile is not part of the InputStream or OutputStream hierarchy. It has no association with those hierarchies other than that it happens to implement the DataInput and DataOutput interfaces (which are also implemented by DataInputStream and DataOutputStream).
It doesn’t even use any of the functionality of the existing InputStream or OutputStream classes – it’s a completely separate class, written from scratch, with all of its own (mostly native) methods. The reason for this may be that RandomAccessFile has essentially different behavior than the other IO types, since you can move forward and backward within a file.
Essentially, a RandomAccessFile works like a DataInputStream pasted together with a DataOutputStream and the methods getFilePointer( ) to find out where you are in the file, seek( ) to move to a new point in the file, and length( ) to determine the maximum size ofthe file.
In addition, the constructors require a second argument (identical to fopen( ) in C)indicating whether you are just randomly reading (“r”) or reading and writing (“rw”).
There’s no support for write-only files, which could suggest that RandomAccessFile might have worked well if it were inherited from DataInputStream.
The seeking methods are available only in RandomAccessFile, which works for files only.
Let’s have a look at how the RandomAccessFile will work by the help of an example.
// Read/write to a Random Access file package randomaccess; import java.io.RandomAccessFile; /** * * @author Administrator */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) throws Exception{ // TODO code application logic here RandomAccessFile ras = new RandomAccessFile("ras.txt","rw"); // We can also pass File & fileDescriptor objects to fis. byte b[] = {1,2,3,4,5,65,6,3,33,52,32,2,33,4}; //photo writeRecord(ras,"x",100,10000f,b); System.out.println("Record pointer is at :" + ras.getFilePointer()); writeRecord(ras,"y",101,10000f,b); System.out.println("Record pointer is at :" + ras.getFilePointer()); writeRecord(ras,"a",102,10000f,b); writeRecord(ras,"b",103,10000f,b); writeRecord(ras,"c",104,10000f,b); printRecords(ras,1); printRecords(ras,2); printRecords(ras,20); ras.close(); } private static void writeRecord(RandomAccessFile r, String name, int eno,float sal, byte photo[]) throws Exception { r.writeUTF(name); r.writeInt(eno); r.writeFloat(sal); r.writeUTF(photo,0,photo.length); r.writeUTF("\n"); } private static void printrecords(RandomAccessFile r, int recno) throws Exception { long curpos = r.getFilepointer(); //set the file pointer to appropriate position long newpos = (recno-1)*30; r.seek(newpos); System.out.println("r.readUTF() + " "); System.out.println("r.readInt() + " "); System.out.println("r.readFloat() + " "); //System.out.println(ras.readBytes(phto,0,photo.length); //ras.writeString("\n"); r.seek(curpos); } }
That's all for this time folks !, we will meet once again with some interesting topics.
Labels:
Files and Streams
nice blog. i also wrote one blog on java ,jsp,servlets,javabeans using mvc architecture.visite my blog
www.javabuzzu.blogspot.com