Solved

Executing code when process finishes

Posted on 2004-10-05
8
173 Views
Last Modified: 2010-03-31
Hi all,

I have an application which starts by reading data from a file.  When it ends, I want it to write data to the file, ready for next time.

What is the best way to do this?  More specifically:

1) How do I make the application execute code on quitting

and

2) Whats the best way to write over data in a file?  Delete the file then create a new one?
0
Comment
Question by:agrees
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12229559
1. You can make the application run the ccode at the end of main
2. Nothing special required - the file will be overwritten anyway
0
 

Author Comment

by:agrees
ID: 12229703
> 1. You can make the application run the ccode at the end of main

It's has a GUI so the application ends when the user closes one of the windows in a sequence of windows I create.  

My current best is idea to add a windowclosed listener to all the windows, but is there a better way?

2. Nothing special required - the file will be overwritten anyway

ok, so just something like this?:

    try {
        BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
        out.write(myData);
        out.close();
    } catch (IOException e) {
    }

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 20 total points
ID: 12229730
>>My current best is idea to add a windowclosed listener to all the windows, but is there a better way?

Good idea

2. Yes, that'll do fine
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 12232412
Create an exit method that saves your file and exits the application.
Then whenever an event occurs that should close the application (eg. exit selected in menu) then call that exit() method.
0
 

Expert Comment

by:ranaanju
ID: 12234807
1. write your code at the end of the main function  in the finally block.
2. open file in the write mode, it will automatically delete if it exist and create it as new file.
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 105 total points
ID: 12235209
Yet another solution: add a shutdown thread to your program,
just in case you exit from an unexpected spot:

     Runtime.getRuntime().addShutdownHook(new Rip());

    ......


    public class Rip extends Thread
    {
   // what must be done when I die ....
    }

;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12242417
8-)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12246138
What kind of expression is that CEHJ?  :<)
;JOOP!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tomcat administrtor 12 68
How to convert from xls to xlsx using java 7 84
Glassfish admin console not working 1 56
Java array sort 10 29
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question