This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes writing to a file using BufferedWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "writing to a file using BufferedWriter" Watch "writing to a file using BufferedWriter" New topic
Author

writing to a file using BufferedWriter

Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57


I run the above program in eclipse giving a string as input.
It's creating the file but is not writing into it.
I am not able to understand what is the reason for it?

Thanks
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Try closing your BufferedWriter instance after you've finished writing to it.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2380
    
    7

Try moving the flush() outside the while loop.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57
I closed the BufferedWriter instance after it is supposed to finish
writing into the file i.e after the while loop.

Yet its not working still
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

your program is fine to me. any error ? where is your output.txt exactly?
Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57
its created in the project root directory after i run the program
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Rajiv Rai wrote:I run the above program in eclipse giving a string as input.

What do you mean by 'giving a string as input' ?
Are you passing a string as a command line parameter or are you typing it in whilst the program is running ?
Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57
I am passing the string as parameter before
running the program using Eclipse run configuration.
So I assume its taken as command line parameter only

Am not giving input whilst the program is running
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Rajiv Rai wrote:I am passing the string as parameter before
running the program using Eclipse run configuration.
So I assume its taken as command line parameter only

Am not giving input whilst the program is running

Command line parameters are passed in the args parameter of your main method.
Trying to read from System.in means your program will read what you type after the program has started.
Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57
Thanks a lot.

Its working now.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38474
    
  23
You do not normally need to flush a writer before closing it, as you will see here.
Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57
@ Campbell

As suggested by you.. I omitted the call to flush()
and instead just closed the BufferedWriter instance
I expected the characters to be written to the file
but that is not the case

Here is the code

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: writing to a file using BufferedWriter