Solved

Executing code when process finishes

Posted on 2004-10-05
8
172 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Suggested Solutions

Title # Comments Views Activity
how to add new optional parameter to JSP 1 49
spring jars download 1 35
Adding multiple JVM environments to RedHat 6 7 47
ejb mdb examples 1 8
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

856 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