Randomly Accessing Files (1)

12 December 2007

I have seen a lot of Java guys struggling with accessing a file and playing the data in it. File input output operations are obviously slow as compared to “in memory” operations but then you have persistent data which is of good use. Someone can argue that databases should be used for input and output but then it’s a separate debate. Just to keep it short, I would say that sometimes it is better to use text files for input and out.

In this post, I will write about randomly accessing files, reading and writing data to the files. Java provides a class named RandomAccessFile which is part of java.io package for this purpose.

Following two constructors are available
RandomAccessFile(File file, String mode)
RandomAccessFile(String name, String mode)

So we can create object of RandomAccessFile either my mentioning the filename (with path) or my giving the name of File object. There are 4 different modes which are as follows:
r – to open file for reading
rw . to open file for reading and writing
rws – to open file for reading and writing and also to write every update to file’s content or metadata synchronously to the underlying storage device.
rwd - to open file for reading and writing and also to write every update to file’s content synchronously to the underlying storage device.

Lets use both the constructors for better understanding.

File file = new File("C:\\demo.txt");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
RandomAccessFile raf = 
   new RandomAccessFile("C:\\demo.txt", "rw");

So far so good. We have RandomAccessFile. Time for a simple example.

demo.txt

Welcome to Java World.
	File file = new File("C:\\demo.txt");
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        byte ch = raf.readByte();
        System.out.println("First character: " + (char)ch);
        raf.seek(0);
        System.out.println("First Line: " + raf.readLine());
        raf.seek(file.length());
        raf.write(0x0A);
        raf.writeBytes("This line is appended by our Java application.");
        raf.close();

Output:

First character: W
First Line: Welcome to Java World.

After execution, demo.txt is altered. The updated file will have following contents:

demo.txt (updated)

Welcome to Java World.
This line is appended by our Java application.

We first created a RandomAccessFile object with read and write mode. We read first character and first line using readByte() and readLine() methods. To move the pointer, seek() method is used. At the end, we wrote a line into the file.

del.icio.us:Randomly Accessing Files (1)  digg:Randomly Accessing Files (1)  spurl:Randomly Accessing Files (1)  wists:Randomly Accessing Files (1)  simpy:Randomly Accessing Files (1)  newsvine:Randomly Accessing Files (1)  blinklist:Randomly Accessing Files (1)  furl:Randomly Accessing Files (1)  reddit:Randomly Accessing Files (1)  fark:Randomly Accessing Files (1)  blogmarks:Randomly Accessing Files (1)  Y!:Randomly Accessing Files (1)  smarking:Randomly Accessing Files (1)  magnolia:Randomly Accessing Files (1)  segnalo:Randomly Accessing Files (1)  gifttagging:Randomly Accessing Files (1)

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Randomly Accessing Files (1)

Leave a Reply