• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

writing to a file using BufferedWriter

 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try closing your BufferedWriter instance after you've finished writing to it.
 
K. Tsang
Bartender
Posts: 3348
12
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try moving the flush() outside the while loop.
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your program is fine to me. any error ? where is your output.txt exactly?
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its created in the project root directory after i run the program
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.

Its working now.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not normally need to flush a writer before closing it, as you will see here.
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic