Results 1 to 3 of 3
- 05-11-2009, 05:40 PM #1
Member
- Join Date
- Mar 2009
- Location
- Atlantis
- Posts
- 8
- Rep Power
- 0
[SOLVED] Read/Write to same XML file
I wrote a program that uses a SAX parser to go through a document, replace a certain word with a user specified one, and then re-write it to the same file. The only problem is that I'm getting a
"SAXParseException: Premature end of file.Error: null"
error when I try to overwrite the current file.
I have:
FileReader file_in = new FileReader(arg[0]);
theReader.parse(new InputSource(file_in));
in my main program
but then in my actual parser class in order to write to the file I have
BufferedWriter out = new BufferedWriter(new FileWriter(filename,false));
I assumed that by giving the FileWriter the flag to overwrite that everything should work, alas I was wrong. The program works as it should if I do something like:
String newFile = "NEW" + filename;
and then pass it to FileWriter, but when it's reading/writing to the same file, it freaks out.
Any suggestions?
- 05-11-2009, 05:48 PM #2
You can't read and write to the same file at the same time, unless you use a RandomAccessFile. You have to close the Reader before you open the Writer.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-12-2009, 12:51 AM #3
Member
- Join Date
- Mar 2009
- Location
- Atlantis
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
[SOLVED] write&read a binary file
By tOpach in forum New To JavaReplies: 9Last Post: 05-01-2009, 12:28 AM -
How to read and write to a file without taking out the comments in the file
By MAGNUM in forum New To JavaReplies: 5Last Post: 02-05-2009, 10:28 AM -
How to read from and write to .properties file from a jsp
By MAGNUM in forum New To JavaReplies: 5Last Post: 01-20-2009, 09:08 AM -
Read and Write file
By mrdestroy in forum New To JavaReplies: 13Last Post: 10-31-2008, 12:11 PM -
File read/write problems
By p900128 in forum New To JavaReplies: 4Last Post: 06-27-2008, 12:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks