Meaningless Drivel is fun!*
The moose likes I/O and Streams and the fly likes Random Access File - Overwrite Contents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Random Access File - Overwrite Contents" Watch "Random Access File - Overwrite Contents" New topic
Author

Random Access File - Overwrite Contents

Udaya Bascar
Ranch Hand

Joined: Jun 18, 2002
Posts: 32
hi friends,
i have an requirement such that i have to overwrite the contents
of the text file
eg
B001 MasteringJava
B002 MasteringEJB
now i want to modify B002 row as
B002 MasteringJSP
when i tried it created an new row instead of overwriting
B001 MasteringJava
B002 MasteringEJB
B002 MasteringJSP
what i actually need is
B001 MasteringJava
B002 MasteringJSP
how can i acheive this
can someone help me out with some code
Regards
Baskar
SCJP
Idly Vada
Ranch Hand

Joined: Sep 02, 2003
Posts: 135
sample code:

import java.io.*;
class FileTest{

public static void main(String args[])throws Exception{
//Open file in read-write mode
RandomAccessFile file=new RandomAccessFile("contents.txt","rw");

//write the contets
file.writeBytes("300 MasteringJSP");

file.close();

}
}
Gayathri Prasad
Ranch Hand

Joined: Jun 25, 2003
Posts: 116
Hi,
There is a good example and detailed explanation of the Random Access File at http://www.javaworld.com/javaworld/jw-01-1999/jw-01-step.html.
Might be of some use.
Gaya3
Udaya Bascar
Ranch Hand

Joined: Jun 18, 2002
Posts: 32
hi friends,
Thanx friends,let me make my requirement very clear, i am running through the file
until the exact book code is found, then i want to overwrite the bookName
public void modifyBookDetails(String str){
try{
String bookCode="B0000002";
String bookName= "MasteringJSP";
File f=new File("F:\\javapro\\BookMaster2.txt");
RandomAccessFile raf=new RandomAccessFile(f,"rw");
String line=raf.readLine();
while( line != null){
line=raf.readLine();
if (line != null){
String s=line.substring(0,8);
if (s.equals(bookCode)){
bookMas=bookCode+" "+bookName;
long filePos=raf.getFilePointer();// line here correct?
raf.seek((filePos);// line here correct?
raf.writeBytes(bookMas);
}
}
}
raf.close();
raf=null;
}
catch(Exception e3){
System.out.println("error in reading file"+e3.toString());
}
}
what I have in my Text File
BookCode BookName
B0000001 MasteringJava
B0000002 MasteringEJB
I want to overwrite B0000002 row such that now my text file will be
BookCode BookName
B0000001 MasteringJava
B0000002 MasteringJSP
I have used the above code what changes i have to do the code to meet my requierment
hi gay3 i could not reach to the link u gave to me becoz its saying its an invalid URL
Regards
Baskar
Idly Vada
Ranch Hand

Joined: Sep 02, 2003
Posts: 135
Hi Baskar !
First advice i would like to give is to take care of lines having varying lengths.
Eg:
100 Mastering EJB
200 Mastering Servlets
if you override line "200 Mastering Servlets" with
"300 Mastering JSP" the file contents will be
100 Mastering EJB
300 Mastering JSPvlets
you shoulld take care of this either by having fixed length lines or by adding necessary white spaces.

There'z a smal problem in ur code gettng file pointer. You should store file pointer before modefying the line.
The modified code is :
import java.io.*;

class FileTest{

public static void main(String args[])throws Exception{

//Open file in read-write mode
RandomAccessFile raf=new RandomAccessFile("contents.txt","rw");

String bookCode ="200";
String bookName = "Mastering JSP ";
String bookMas;

String line=raf.readLine();

while( line != null){

long filePos=raf.getFilePointer(); //change here

String s=line.substring(0,3);

if (s.equals(bookCode)){
bookMas=bookCode+" "+bookName;
raf.seek(filePos);
raf.writeBytes(bookMas);
}
line=raf.readLine();
}
raf.close();

}
}
Ste Graham
Greenhorn

Joined: Sep 15, 2003
Posts: 23
Hi People - I thought that I'd add to this thread rather than create another - it seems to touch what I'm struggling with. I too have a config file which looks something like:

name1=value1
name2=value2
name3=value3

etc etc

Now, if I want to changed the value of name2, what is the best way of doing it? I can find the value I want to change by searching on the name. The below, obviously wrapped in a try/catch etc is what I'm working with at the moment:



The main problem is, what happens when the new value is longer that the original value. Yes, that's right - the next name/value pair on the next line is overwritten - either completely or partially (depending on the length of the new value). How is it possible to "drop in" a value. It seems that all subsequent values need to "bumped along" by x bytes. I've been looking at the various classes but can't see which one would be the best to do this. Any help/advice would be much appreciated!

Cheers, Stephen

[ November 02, 2004: Message edited by: Ste Graham ]
Mark Patrick
Ranch Hand

Joined: Feb 22, 2004
Posts: 51
Steven,

It looks to me like you have a file of properties and values. Look at the Properties class. It has convenient methods built in to handle reading and writing properties.


Mark Patrick<br />SCJP 1.4
 
jQuery in Action, 2nd edition
 
subject: Random Access File - Overwrite Contents