Java Examples - Delete File

Java 实例Java Examples

The following example demonstrates deleting a file using the delete() method:

Main.java File

import java.io.*; public class Main { public static void main(String[] args) { try{ File file = new File("c:\\test.txt"); if(file.delete()){ System.out.println(file.getName() + "File has been deleted!"); }else{ System.out.println("File deletion failed!"); } }catch(Exception e){ e.printStackTrace(); } } }

The output of the above code is (you need to create a test.txt file on the C drive first):

test.txt 文件已被删除!

Java 实例Java Examples

Other Extensions