• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RandomAccessFile: backspacing and inserting

 
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


import java.io.*;
public class MainClass
{
public static void main(String args[])
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));

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

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

System.out.println(s);

System.out.println("new?");
String newStr = br.readLine();

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

for(int i=1; i<=l; i++)
{
raf.writeBytes("\b");
}

raf.writeBytes(newStr);
}

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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic