Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

reopening an output stream?

 
Ranch Hand
Posts: 543
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: 543
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
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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: 543
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: 543
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
 
Marshal
Posts: 69874
278
  • 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: 69874
278
  • 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.
     
    mooooooo ..... tiny ad ....
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic