Solved

Executing code when process finishes

Posted on 2004-10-05
8
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

624 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