Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FileIO

 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//Date 22/06/2010
Programmer : Sumit Kothalikar


line 1 writes the quoted text in Shree.txt file
even though the text is read at line 2 it worksout to be that s is null at line 3 and
there is no output
my question is why s becomes null at line 3 what should be done to read Shree.txt file
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 1 writes your text to the BufferedWriter - not the file. You need to call flush() and/or close() to write the text to the file. Then you can read it. Try moving bw.close() to just after line 1.
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Sir,
I did it as you said in line new but the program goes into infinite loop after running the above code.
I can't understand why is it happening so.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are initializing s properly but not updating it in your loop so s is never being set to null. Try adding

to your while loop.
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I changed the while code at line 3
but now it doesn't print anything.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we've now fallen into the trap where you ask a follow-up question without first thinking it through yourself. Your problem now is not a misunderstanding about how an API works. It is a simple bug in your code.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never do this:

You simply must handle IO exceptions; at least print a message to the log or system out so you know something bad happened.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's help him



Check this....
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Never do this:

catch(IOException e){}


catch(IOException e){} }
You simply must handle IO exceptions; at least print a message to the log or system out so you know something bad happened.



Yes i got it what you want to say Sir
i will always include a System.out.println(e) statement in catch block to through the exception.

But please tell me is there any bug in my code
i am not getting why it is not printing the text which i am writing to the file Shree.txt.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sumit kothalikar wrote:
I changed the while code at line 3
but now it doesn't print anything.


Try this code, I made a correction,



Clue: readLine() method read& returns the next line in your text.
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Abimaran Kugathasan Sir
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need of Sir!
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks Abimaran
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic