热门文章 | 热门软件| 热门源码 | 热门电影 | 知识库 | 联系我们
软件 源码 教程 影视 健康 招聘
  HTML | JavaScript | ASP | PHP | JSP | NET | VB | VC | VF | Windows | Linux | Mysql | Mssql | Oracle | Struts 
当前位置: 创世纪计算机资源网 -> 文章频道 ->java 
站内搜索:
怎么用java删除文件??
作者:佚名 来源:csdn 整理日期:2007-3-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();
}
}
}



 

相关文章