Solved

Executing code when process finishes

Posted on 2004-10-05
8
169 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
Comment Utility
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
Comment Utility
> 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
Comment Utility
>>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
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Expert Comment

by:ranaanju
Comment Utility
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
Comment Utility
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
Comment Utility
8-)
0
 
LVL 24

Expert Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now