Meaningless Drivel is fun!*
The moose likes I/O and Streams and the fly likes Does Not Write To A File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Does Not Write To A File" Watch "Does Not Write To A File" New topic
Author

Does Not Write To A File

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I have this little program (shown below). The program gives me the expected output and I can print the output to the console
System.out.println(result);
I also want to write the output to a file. However, the program does not write anything to the file I specified. I got a blank file.
What mistake did I make?

[ December 31, 2002: Message edited by: JiaPei Jen ]
[ December 31, 2002: Message edited by: JiaPei Jen ]
[ December 31, 2002: Message edited by: JiaPei Jen ]
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1121

Hi Jen,
Assuming that you are executing your java class on a Windows machine, the only mistake I saw in your code was the name of the file you are trying to write to. The correct line of code should be:

Hope this helps you.
Good Luck,
Avi.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
I think you need to flush your output stream before you are finished, otherwise the buffered bytes are not written.


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Closing the output file when you're done with it would be a good idea - that will also flush it. But I don't think that's the biggest problem here. You're creating a new FileOutputStream each time you find a match to the pattern "Administrative Contact". Because you use the constructor new FileOutputStream(File), the new stream will overwrite any previous file, effectively replacing it with a new file that contains only the most recent information. To avoid this, you have two options:
(1) Use the constructor new FileOutputStream(File, boolean) instead, setting the second argument to true in order to append rather than overwrite. E.g. "new FileOutputStream(fout, true)".
(2) Create the FileOutputStream outside the loop, so it just happens once, and each time out.write() is called inside the loop it's appended to the same file. Then close the stream once after the loop is completed.
The second method is probably what you want here. Note that it's also possible to do a number of other things before the loop, such as compiling the Pattern (since it's always the same, each time through the loop).
Also, are you sure that the pattern is even being found in the data you're looking at? Is the line

causing something to be printed? You may want to modify it a bit to give it a more distinctive label to be sure:


"I'm not back." - Bill Harding, Twister
gautham kasinath
Ranch Hand

Joined: Dec 01, 2000
Posts: 583
flush the stream after every write operation..
out.write(what-ever);
out.flush();
the when you are closing the inputstream object close the out too.
If you do not close or flush, the data will be only in the stream, but not written to disk.
Regds
Lupo


"In the country of the blind, the one eyed man is the King"
Gautham Kasinath CV at : http://www.geocities.com/gkasinath
 
 
subject: Does Not Write To A File
 
Similar Threads
Holy SPANK!! I got a connection...
reading a file from my web server using an applet
java.io.IOException: Premature EOF
Write To A URL and Communicate With The Host Server
different behaviour in writing to a file