boolean success = (new File("filename")).delete();
if (!success) {
// Deletion failed
}
*************************************************
import java.io.IOException;
public class Test{
public static void main(String[] args)
{
String filepath="你要删除的文件的绝对路径";
Runtime rt = Runtime.getRuntime();
try {
rt.exec("del "+filepath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
*************************************************
flag 返回false,是因为没有这个要删除的文件(D:\\wgh\\lesson5\\wgh.txt).
你创建一个吧。
import java.io.File;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
String filepath = new String("D://Media Files/Backstreet Boys/test.txt");
Runtime rt = Runtime.getRuntime();
File file = new File(filepath);
try {
file.createNewFile();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
***********************************************
试一下这一个吧,我在我的电脑上运行过可以的。把c:\\test.txt替换为你要删除的文件
import java.io.IOException;
public class CSDN4 {
public static void main(String[] args)
{
String filepath="c:\\test.txt";
Runtime rt = Runtime.getRuntime();
try {
rt.exec("cmd /c del "+filepath);
} catch (IOException e) {
e.printStackTrace();
}
}
}