• Post Reply Bookmark Topic Watch Topic
  • New Topic

reopening an output stream?  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I'm making a program to create a roster for a boxing promotion it will keep data on each fighters name height weight etc, I want to get the input from a user and enter it to a be stored in a text file,the only problem is after I add the fighter's details I have to close the stream to make sure a resource leak does not happen,there is not much I could find online but from what I read when you close an output/input stream you cannot reopen it,

the problem is that I need to keep writing to the same file,without overwriting it ofcourse,

any ideas what I can do?

thanks I'll post the code below it's not too long

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also here is the stacktrace


Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedWriter.ensureOpen(Unknown Source)
at java.io.BufferedWriter.write(Unknown Source)
at java.io.Writer.write(Unknown Source)
at Main.addFighter(Main.java:52)
at Main.main(Main.java:30)

another weird thing is that I am allowed enter two fighters in but when it gets to the third that's when the exception occurs

thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Have you tried opening the file with append mode? ... see the other constructors for the FileWriter class that you are using.

Henry
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




I changed it to true but I'm not sure where to go from here
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Henry I changed where I declared the FileWriter,I removed it from been a static global variable to inside the addFighter method,still have a lot of work to do,is there any books you recommend on these type of things like input/output,or intermediate books which covers this stuff I'm struggling to find much resources on the web,

thanks
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best book I have read is Core Java II formerly written by Cay Horstmann and Gary Cornell, but the latest edition is by Horstmann alone. Consider a secondhand copy of the 9th edition if you are short of cash. Whichever you buy, make sure to get an edition covering NIO. You can probably get by with Volume I only at this juncture.
Warning: Horstmann's style is quite unlike that of Head First Java; it isn't breezy and chatty. Go through the “Look Inside” feature on Amazon before buying anything.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:. . . FileWriter . . . from been a static global variable to inside the addFighter method . . .
A few general rules of thumb:-
  • 1: The more fields you have the more chance there is for things to go wrong.
  • 2: Never make global fields unless they are constants, this being an example. All other fields shou‍ld have private access.
  • 3: Iterators, Streams, Readers, Writers, Scanners, Formatters, etc. shou‍ld all be created with local scope and closed when they are finished with (if they can be closed at all). The exception is things using System.in, System.err and System.out, which must never be closed.
  • I think things reading from System.in and similar shou‍ld be created in their own classes because you usually don't need multiple objects to read System.in.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!