aspose file tools*
The moose likes Java in General and the fly likes Writing to an already created RandomAccessFile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Writing to an already created RandomAccessFile" Watch "Writing to an already created RandomAccessFile" New topic
Author

Writing to an already created RandomAccessFile

Priyha Jootu
Ranch Hand

Joined: Sep 13, 2001
Posts: 47
Hi all,
I need to read a file and search for a string in it and if the string equals certain value, then i need to write a line in the file and close it.
The file to be written already exists. RandomAccessFile is not allowing me to read a file that already exists.
I have this line in my code:
String f = "C:\\soe\\SOE_Arrays2.js";
File arrayfile = new File(f);
RandomAccessFile raf = new RandomAccessFile(arrayfile, "rw");
It gives me an error that cannot create a file that already exists. I dont want to create anew file. I want to open a existing file and make changes to it.
My requirement is:
The file to write : SOE_Arrays2.js
file contents look like this:
var os2 = [
["alp","Alpha VMS"],
["lnx","Linux"]
];
var letter = [
["new","New"],
["addon","Add-on"],
["lsf","LSF"],
["renew","Renewal"]
];
I will get a new Operating system from the user as input and add to the already existing array above using the write() methods in random access file. so I will search for a string like
"var os2 = ["
in the above file and insert a line like this:
["64w","64-bit Windows"], at that location.
Please let me know how to do this.
Thanks,
Priyha.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Interesting. You should be able to open any existing file and read and write to it. What is the exact error that you are getting?


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Priyha Jootu
Ranch Hand

Joined: Sep 13, 2001
Posts: 47
I am using this code in a jsp file
The error is:

If we can read and write to existing file, then is it possible to do the requirement i was telling previously. It is very critical that i need to do this for a project. If possible, can you send me some sample code which reads and writes to an existing file using RandomAccessFile?
Thanks a lot,
Priyha
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Ah, you're doing this in a JSP! You probably need to post this in the JSP/Servlet forum. Are creating the RAF directly in the JSP? If so, you should probably consider using a bean instead. I'm not sure what restrictions the Web container places on the java.io package but would guess that you're problem is there.
Priyha Jootu
Ranch Hand

Joined: Sep 13, 2001
Posts: 47
As you said, I wrote a seperate java program to do read and display the contents of RAF. It worked. Now, my problem is I am not able to insert any string in the RAF at a particular location, it always overwrites the already present string.
The java prog. is here:

In the SOE_Arrays2.js file, i have this :
os1 = [
["win","Windows"],
["unx","UNIX"],
["mvs","MVS"]
];

os2 = [
["alp","Alpha VMS"],
["lnx","Linux"]
];
var letter = [
["new","New"],
["addon","Add-on"],
["lsf","LSF"],
["renew","Renewal"]
];

After executing the code SOE_Arrays2.js looks like this:
os1 = [
["win","Windows"],
["unx","UNIX"],
["mvs","MVS"]
];

os2 = [["64w","64-bit Windows"],
lnx","Linux"]
];
var letter = [
["new","New"],
["addon","Add-on"],
["lsf","LSF"],
["renew","Renewal"]
];
you can see that RAF overwrites to the file at that particular location( after the line "os2 = [ ". What i need is to insert the text at a particular location, Is there a way to do this?
I tried to insert carriage return, etc and move the file ptr back a little and write, whatever it is, RAF overwrites it, is there a way to insert instead of overwrite?

Please let me know.
Thanks,
Priyha.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing to an already created RandomAccessFile