aspose file tools*
The moose likes Java in General and the fly likes Problem writing a string to a file(Urgent) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem writing a string to a file(Urgent)" Watch "Problem writing a string to a file(Urgent)" New topic
Author

Problem writing a string to a file(Urgent)

rudolf hitler
Ranch Hand

Joined: Feb 07, 2003
Posts: 34
how do i write a string to a file.I have tried the following but it doesnt seem to work .Can you let me now what the problem might be.
File f=new File("archive.txt");
FileWriter fw=new FileWriter(f,true);
BufferedWriter bw=new BufferedWriter(fw);
String a="aaaaa";
bw.write(a);
I tried this once it worked but when i ran my program a second time it just opens the file archive.txt but does not write anythin into it.
I tried converting the string into an array of characters and then writing it only using file writer but it didnt seem to work even then.
What might the problem be?
Hari Gangadharan
Ranch Hand

Joined: Mar 08, 2001
Posts: 73
Flush it after you use it

I mean you need to do:

Hope this helps.


<B>Hari Gangadharan</B><BR>Unix is user friendly..<BR>but it chooses to whom it is friendly with!
rudolf hitler
Ranch Hand

Joined: Feb 07, 2003
Posts: 34
thanx a lot for the help but can u explain how come it worked when i ran it the first time and then stopped workin if it is coz of the flush problem
Hari Gangadharan
Ranch Hand

Joined: Mar 08, 2001
Posts: 73
Java ensures that the files are closed on exit. In the current case the BufferedWriter wraps your FileWriter. BufferedWriter's default behaviour is to flush the buffer to file once in a while. So if you are using a buffered file writer and if you do not explicitly call flush then there is chance that some of the stuff written is not physically written to disk. You can experience this when you are writing huge files (the last chunk may be missing). So I would assume that the first time it happened because the buffered writer flush executed before the file writer is closed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem writing a string to a file(Urgent)
 
Similar Threads
Trouble appending new data to an existing file
Doubt about IO package
Question about File, FileWriter + BufferedWriter
writing to a file
Buffered Writer problem