Randomly Accessing Files (3)

13 December 2007

Writing to a text file using RandomAccessFile will overwrite the text if the text is written before the end of file. I will present an example to prevent loss of data.

Please go through my previous posts on this topics
- Randomly Accessing Files (1)
- Randomly Accessing Files (2)

We have a Text file with following contents:
demo.txt

If there is a security manager, its checkRead method is called with the pathname of the file argument as its argument to see if read access to the file is allowed.
If the mode allows writing, the security manager’s checkWrite method is also called with the path argument to see if write access to the file is allowed.
File ends here.

We want to add “This line is added by application.” Just after the first line. Lets see how to do this.

		File file = new File("C:\\demo.txt");
		RandomAccessFile raf = new RandomAccessFile(file, "rw");
		String str = raf.readLine();
 
		int size = (int)raf.length() - str.length();
		byte[] myByteArray = new byte[size];
 
		raf.read(myByteArray);
		raf.seek(str.length());
		raf.write(Character.LINE_SEPARATOR);
		raf.writeBytes("This line is added by application.");
		raf.write(Character.LINE_SEPARATOR);
		raf.write(myByteArray);
 
		raf.close();

demo.txt (updated)
If there is a security manager, its checkRead method is called with the pathname of the file argument as its argument to see if read access to the file is allowed.
This line is added by application.
If the mode allows writing, the security manager’s checkWrite method is also called with the path argument to see if write access to the file is allowed.
File ends here.

Required results are achieved. The text is added at the right place without any lose of previous information. How this is done? Let me briefly explain. First I created a RandomAccessFile object with rw mode. The file was opened and its first line was read and stored in a string object. Then we created a byte array with size :

No of characters in the file – No of characters in first line (str)

The file pointer is pointing at the end of first line. We read all the contents from the current position (after first line) and saved them into byte array. Now pointer is pointing to the end of file. Pointer was moved to the end of first line using seek method and the required line was written to the file. And then finally, the byte array containing the text after the first line was written to the file.

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

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 (3)

Leave a Reply