设为首页   |  加入收藏夹 快速导航:  热门文章  |  最新文章  |  梦想博客  
当前位置:编程之家 -> 文章频道 ->java 
站内搜索:  

怎么用java删除文件??

作者:佚名 来源:csdn 整理日期:2007-03-29
系统上原有的文件,怎么删除,用File.delete(),File.deleteOnExit()都不行,不知道怎么搞的?
那些文件不是只读的啊?

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();
}
}
}