File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes RandomAccessFile: backspacing and inserting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "RandomAccessFile: backspacing and inserting" Watch "RandomAccessFile: backspacing and inserting" New topic

RandomAccessFile: backspacing and inserting

Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

public class MainClass
public static void main(String args[])
BufferedReader br=new BufferedReader (new InputStreamReader(;

File f = new File("Test.txt");
RandomAccessFile raf = new RandomAccessFile(f,"rw");

String s = raf.readLine();
s = raf.readLine();


String newStr = br.readLine();

int l = s.length();
System.out.println("length: " + l);

for(int i=1; i<=l; i++)


catch(Exception e){

This is just a practice program. It is meant to replace the second line with a new record. But instead of backspacing "\b" it overwrites the third line and when it writes "newStr" it also overwrites.

This is totally not what I'm trying to get. I want to remove the second line by backspacing it and then insert a new string in its place without touching the third line.

How do I do this?

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
I haven't used RAF, but I very much doubt that it can deal with backspace characters - it's not a console or text editor, after all.

If you want to put a different character at the start of line 2 you need to set the file pointer to that position (using seek), and then you can start overwriting.
[ February 25, 2008: Message edited by: Ulf Dittmer ]
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Yes but the only problem with that is that it needs to be the EXACT number of characters. If it's one of too little then there's some characters left over. And if there's too many then it overwrites line 3
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Yes, unfortunately RAF really isn't good for this sort of thing. If you want to replace one region with another string with a different length, you need to shift all the subsequent bytes in the file to accommodate the new length. There's no simple method call to do that, and it's a fairly ineffecient process - especially if you still plan to edit the file more afterwards.

Generally if you want to do this sort of thing, you'd be better off keeping the file in memory as, say, an ArrayList if String objects, each one representing one line. Or perhaps a LinkedList, or perhaps containing StringBuilder objects instead of Strings. Make your changes to the objects in memory, and then, when you're done with all the changes, write a new file, probably using PrintWriter. (Or if you're on a JDK prior to JDK 5, using a PrintWriter wrapped in a BufferefWriter wrapped in a FileWriter - blech.)

Incidentally, having an empty catch block there is a very bad idea, even in a practice program. If anything at all goes wrong, you will hide the error and waste time because you can't see what happened. Putting a simple e.printStackTrace() in the catch block can save you a lot of time later.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: RandomAccessFile: backspacing and inserting
jQuery in Action, 3rd edition