Results 1 to 6 of 6

Thread: Delete files when closing window

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    Delete files when closing window

    Hello,
    I wish that when I close the window of my program Java, I delete the files that I created. Here is the code for it, which I am being trying.
    Code:
    frame.addWindowListener(new WindowAdapter()
        	{
        	    Public void windowClosing(WindowEvent e)
        	    {
        	    	File val_fl = new File("C: / Documents and Settings/temp/Desktop/val_fl");
        File parm = new File("C: / Documents and Settings/temp/Desktop/parm");
        File dir = new File("C: / Documents and Settings/temp/Desktop/dir");
        val_fl.delete();
        parm.delete();
        dir.delete();
        	    }
        	});
    But the following code does not work. Do you have any ideas about it.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Delete files when closing window

    Hello,
    Code:
    But the following code does not work. Do you have any ideas about it.
    The file is probably still open in your application. At a pinch you could use deleteOnExit () instead.
    Caution : Do not use deleteOnExit () On an application server or a large amount of data because there is a risk of memory leak.
    Why put these files in the directory of your application?
    - Whether it is temporary file, and in this case it is better to use File.createTempFile () to create them.
    - Either there is config file, and in this case it is best to create a folder in the home directory of the user
    Code:
          File fl = new File(System.getProperty("user.home"), . MyApp ");

  3. #3
    Join Date
    Dec 2009
    Posts
    204

    Re: Delete files when closing window

    Hi,
    Thank you for your answers, but in fact I use PrintWriter to create my files where I'm going to write.
    Code:
    PrintWriter valfl = new PrintWriter( new FileWriter("valfl"),true );
    I have not found a method similar to the one you advised me to "File". The code I gave earlier was a sort of "trickery" in order to erase (but it does not work) Or how to create my files with "File" to use your method deleteonclose () and write to it (without changing all my code. I only use PrintWriter and I use simple "valfl . System.out.println () "to write it

  4. #4
    Join Date
    Nov 2009
    Posts
    335

    Re: Delete files when closing window

    Hello,
    I have not found a method similar to the one you advised me to "File".
    You simply pass the file created in your FileWriter, rather than passing the file name
    The code I gave earlier was a sort of "trickery" in order to erase (but it does not work )
    If this does not work that is that your file is still open. Close your file in your code and then try compiling it. Try this and if you have any problem then do post back.

  5. #5
    Join Date
    Dec 2009
    Posts
    204

    Re: Delete files when closing window

    Hello,
    Ok
    - It creates and deletes to the end ==> AC is REAL!
    BUT
    - It no longer writes anything in it .... ==> AC is not cool!
    That's what I did:
    Code:
    File vl = new File(System.getProperty("user.home"), "pw");
    vl.deleteOnExit();
    PrintWriter pw = new PrintWriter(vl);
     
    / / And when I write in:
    pw.System.out.println("test");
    Why not write anything in it?

  6. #6
    Join Date
    Dec 2009
    Posts
    178

    Re: Delete files when closing window

    Hello,
    Even I have a similar kind of a problem, but after some testing and working on the code, I come to knew that I did not close the stream. I had forgot to close the stream in my code. I think even you have did not closed the stream. If you do not know how to then, you have to add
    Code:
    valfl.close();
    Try it and I am sure this will work, if not then post back with the error.

Similar Threads

  1. How to delete temp file in window 7?
    By JeetVerma in forum Networking & Security
    Replies: 2
    Last Post: 17-02-2012, 03:02 PM
  2. Dep keeps closing video files in windows media player
    By neonxgenesis in forum Windows Software
    Replies: 6
    Last Post: 10-11-2009, 03:02 PM
  3. How to Stop Flash .tmp files from auto-deleting when closing
    By Drek in forum Technology & Internet
    Replies: 2
    Last Post: 10-06-2009, 08:35 AM
  4. Window Live Onecare back up files --how to delete
    By Benjamin in forum Windows XP Support
    Replies: 3
    Last Post: 01-06-2009, 12:19 PM
  5. How To Delete Files Which Windows Wont Delete?
    By Hardik in forum Windows Software
    Replies: 2
    Last Post: 06-03-2007, 12:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,065,946.14600 seconds with 17 queries