File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 2584
    
    9

Try moving the flush() outside the while loop.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
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: 39784
    
  28
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

 
wood burning stoves
 
subject: writing to a file using BufferedWriter