• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Does Not Write To A File

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you need to flush your output stream before you are finished, otherwise the buffered bytes are not written.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Evildoers! Eat my justice! And this tiny ad's justice too!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic