Java File delete()方法
java.io.File.delete() 方法删除由抽象路径名定义的文件或目录。若要删除一个目录,该目录必须是空的。
1 语法
public boolean delete()
2 参数
无
3 返回值
如果文件已成功删除,则此方法返回true,否则返回false。
4 示例
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.io.File.delete()方法的例子
*/
import java.io.File;
public class Demo {
public static void main(String[] args) {
File f = null;
boolean bool = false;
try {
// create new file
f = new File("test.txt");
// tries to delete a non-existing file
bool = f.delete();
// prints
System.out.println("File deleted: "+bool);
// creates file in the system
f.createNewFile();
// createNewFile() is invoked
System.out.println("createNewFile() method is invoked");
// tries to delete the newly created file
bool = f.delete();
// print
System.out.println("File deleted: "+bool);
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
}
}
}
输出结果为:
File deleted: true
createNewFile() method is invoked
File deleted: true
热门文章
优秀文章