• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RandomAccessFile not close in DVD sample project

 
Terence Lai
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finished studying the book "SCJD Exam with J2SE 5". I realize that the sample code, DvdFileAccess.java, never close the RandomAccessFile. Is this going to be a problem (causing system resource leakage)? Will I lose mark on the exam if I don't close the RandomAccessFile?

Thanks,
Terence
 
Alenkhe David
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some things that were not done correctly in that DVD project. For example when you put an incorrect file location it throws an exception and the gui disappares while the program is still running; there are others in this manner too.

I would advise you close your random access file because any system resource should be close.
 
Jethro Borsje
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking into this closing in the Monkhouse book, but I also couldn't find it. Where / how should I close the RAF? I mean: at this point the server has no GUI, so should / can I somehow detect that the JVM is being killed or something?
 
Thomas Thevis
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could add a shutdown hook, see the Runtime class for that matter.
 
Justin Rundle
Ranch Hand
Posts: 123
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Jethro] ... Where / how should I close the RAF? ...
I guess this is a design decision, more so Im referring to do you cache your records or not? Consider the following scenario:

If you do cache your records then on startup you could read all the records in and close the stream. Then when saving your record(s) you can open a new output stream, write the records then close the stream, and dont forget to use a finally block to always ensure your stream gets closed

Additionally I have read in other posts that other implementations maintain an open stream for the duration of the program, however I would strongly discourage this.
 
Jethro Borsje
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Rundle:
[Jethro] ... Where / how should I close the RAF? ...
I guess this is a design decision, more so Im referring to do you cache your records or not? Consider the following scenario:

If you do cache your records then on startup you could read all the records in and close the stream. Then when saving your record(s) you can open a new output stream, write the records then close the stream, and dont forget to use a finally block to always ensure your stream gets closed

Additionally I have read in other posts that other implementations maintain an open stream for the duration of the program, however I would strongly discourage this.
I am using a cache, so I could go about the approach you recommend here. It certainly sounds better than keeping the stream open the entire time.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic