Delete a file

ReturnMethodSummary
booleandelete()Deletes the file or directory.
voiddeleteOnExit()Requests that the file or directory to be deleted when the virtual machine terminates.

import java.io.File;
import java.io.IOException;

public class Main {

  public static void main(String[] args) {

    
    try {
      File tempFile = File.createTempFile("abc","txt",new File("c:\\"));
      tempFile.delete();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Use deleteOnExit method:


import java.io.File;
import java.io.IOException;

public class Main {

  public static void main(String[] args) {

    
    try {
      File tempFile = File.createTempFile("abc","txt",new File("c:\\"));
      tempFile.deleteOnExit();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.