• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble writing to a text file

 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Long story short I was given a mundane task at work that a simple program could do much quicker for me. I've got it mostly done, I'm able to read from teh file just fine but I'm having trouble writing to the various files I want to...I think it has to do with the creation of the PrintWriter and the method I'm using. Anyone see anything below:



I know my logic is correct because I tested with System.out first.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, at first sight, you are not flushing your Printwriter. Call the flush() method before closing, or use the Printwriter constructor that sets autoflushing.

I would try that first, but I am sure there maybe a few other recommendations for you besides of this.
[ May 10, 2006: Message edited by: Edwin Dalorzo ]
 
Matt Kidd
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
WOOT! That worked. Thanks Edwin. Now why is it important to use "flush"? As easy as this was it's probably pretty clear I'm missing a few of the key details about the intricacies of I/O....and some may say Java. But I'm still working on it. It's been a rough year so far.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not important to call flush() before closing -- it's important to call close(), which you're not doing. Calling "flush()" mitigates the problem, but the right way to do things is to close a file when you're through with it.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Java IO by Elliotte Rusty Harold.


Many output streams buffer writes to improve performance. Rather than sending each byte to its destination as it's written, the bytes are accumulated in a memory buffer ranging in size from several bytes to several thousand bytes. When the buffer fills up, all the data is sent at once. The flush() method forces the data to be written whether or not the buffer is full [...].

If you only use a stream for a short time, you don't need to flush it explicitly. It should be flushed when the stream is closed. This should happen when the program exits or when you explicitly invoke the close() method [...].

You only need to flush an output stream explicitly if you want to make sure data is sent before you're through with the stream. For example, a program that sends a burst of data across the network periodically should flush after each burst of data is written to the stream [...].

Flushing is often important when you're trying to debug a crashing program. All streams flush automatically when their buffers fill up, and all streams should be flushed when a program terminates normally. If a program terminates abnormally, however, buffers may not get flushed. In this case, unless there is an explicit call to flush() after each write, you can't be sure the data that appears in the output indicates the point at which the
program crashed. In fact, the program may have continued to run for some time past that point before it crashed.

System.out, System.err, and some (but not all) other print streams automatically flush after each call to println() and after each time a new line character ('\n') appears in the string being written. Whether auto-flushing is enabled can be set in the PrintStream constructor.


[ May 10, 2006: Message edited by: Edwin Dalorzo ]
 
Matt Kidd
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
It's not important to call flush() before closing -- it's important to call close(), which you're not doing. Calling "flush()" mitigates the problem, but the right way to do things is to close a file when you're through with it.



so instead of changing the constructor it would be a better idea to close all the output streams just before the end of the try block?

hmm.....

hey that worked too. I need to read the I/O chapter from the book (one of many) that I have so this makes sense.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Edwin Dalorzo:
From Java IO by Elliotte Rusty Harold.



Be careful. For the PrintWriters in use here, it's not true that printing a '\n' calls flush() in auto-flush mode; it was (is) true for PrintStream, however.

It's also not true that the chains of open PrintWriter/BufferedWriter/FileWriters will automatically be flushed on program exit. The underlying file stream will be closed and the OS buffer flushed, but the BufferedWriters are going to be holding some data, and that will be lost unless you flush() or close() the PrintWriters. Nothing is going to automatically close() or flush() them for you.
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would also be best to have your close in a finally block, rather than
at the end of the try block.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic