Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31114 > unrolled thread

Quick Question

Started byDoug Mika <dougmmika@gmail.com>
First post2015-02-11 09:56 -0800
Last post2015-02-14 22:37 -0500
Articles 4 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Quick Question Doug Mika <dougmmika@gmail.com> - 2015-02-11 09:56 -0800
    Re: Quick Question Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-02-11 13:19 -0500
    Re: Quick Question Doug Mika <dougmmika@gmail.com> - 2015-02-11 11:27 -0800
    Re: Quick Question Arne Vajhøj <arne@vajhoej.dk> - 2015-02-14 22:37 -0500

#31114 — Quick Question

FromDoug Mika <dougmmika@gmail.com>
Date2015-02-11 09:56 -0800
SubjectQuick Question
Message-ID<73c71bbc-e620-42f0-9424-6ff70cdefa81@googlegroups.com>
I have code that I need to execute upon the user closing my program.  To be exact I need to close the file stream when the user closes my application.  How can I program this?  There are no destructors in Java, and that was one idea I had but destructors aren't always called?

Any advice is appreciated

Doug

[toc] | [next] | [standalone]


#31116

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-02-11 13:19 -0500
Message-ID<mbg6em$vmc$1@dont-email.me>
In reply to#31114
On 2/11/2015 12:56 PM, Doug Mika wrote:
> I have code that I need to execute upon the user closing my program.  To be exact I need to close the file stream when the user closes my application.  How can I program this?  There are no destructors in Java, and that was one idea I had but destructors aren't always called?

     (I guess you mean "finalizers" by that second "destructors," but it
doesn't make much difference: They're not guaranteed to be called.)

     There are several approaches, suitable for several different kinds
of program structure.  In roughly descending order of ease and general
suitability:

     - If the creation, use, and closing of the file stream can take
place within a single code block (which may, of course, call as many
other methods and things as it likes), consider "try with resources:"

	try (Stream stream = ...) {
	    // lots of code using the open `stream'
	}
	// `stream' is closed automatically when the `try' block
	// completes, either normally or abruptly

     - The "try" approach won't work if method M() opens the stream and
it's to stay open even after M() returns.  So next I'd look at how the
user closes your program: Does he click on a "STOP" button, or type
Ctrl-D, or what?  Add a "close the stream if it's still open" call to
the code that handles each such event, and you're all set.  If the
user halts your program by closing its main window (maybe via an O/S-
supplied little red X or something), you can register a WindowListener
to be informed of the closing event, and have it close the stream.

     - Finally, there's Runtime.addShutdownHook().  With this method
you can arrange to have some code run while the JVM is shutting down
(if "your program stops" implies "JVM shuts down").  Unfortunately,
while it's easy to make the arrangements to run some code, the code
itself is rather tricky to write -- just read the scary-sounding
language in its Javadoc, and you'll see what I mean.

     There may be other possibilities, too, depending on how your
program is structured, how the stream gets opened, and how the user
stops the program.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#31117

FromDoug Mika <dougmmika@gmail.com>
Date2015-02-11 11:27 -0800
Message-ID<47b59d47-89a9-429a-9994-7c692bd63164@googlegroups.com>
In reply to#31114
Thanks, I completely fergot about the WindowListener...that worked.

Doug

[toc] | [prev] | [next] | [standalone]


#31133

FromArne Vajhøj <arne@vajhoej.dk>
Date2015-02-14 22:37 -0500
Message-ID<54e01499$0$287$14726298@news.sunsite.dk>
In reply to#31114
On 2/11/2015 12:56 PM, Doug Mika wrote:
> I have code that I need to execute upon the user closing my program.
> To be exact I need to close the file stream when the user closes my
> application.  How can I program this?  There are no destructors in
> Java, and that was one idea I had but destructors aren't always
> called?

There are many ways t get files closed. And it is good practice
do so.

That said then if you just flush after write, then the OS should
close the file when the program exists for. At least I am not aware
of any OS supporting Java that does not do so.

It is still good practice to close files explicit. But maybe quick and
dirty is good enough for you.

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web