Java天地
java刪除文件和目錄
package
com.word;
import
java.io.IOException;
public
class
DeleteFile
{
public
static
void
main(String[] args)
{
String filepath
=
"
D:\\text.txt
"
;
Runtime rt
=
Runtime.getRuntime();
try
{
rt.exec(
"
cmd /c del
"
+
filepath);
System.out.println(
"
刪除成功
"
);
}
catch
(IOException e)
{
System.out.println(
"
刪除失敗
"
);
}
}
}
package
com.word;
import
java.io.IOException;
public
class
DeleteFile
{
public
static
void
main(String[] args)
{
String filepath
=
"
D:\\text.txt
"
;
Runtime rt
=
Runtime.getRuntime();
try
{
rt.exec(
"
cmd /c del
"
+
filepath);
System.out.println(
"
刪除成功
"
);
}
catch
(IOException e)
{
System.out.println(
"
刪除失敗
"
);
}
}
}
package
com.word;
import
java.io.File;
/** */
/**
*
*
@author
XWZ
* 2007-11-27
* 刪除文件或目錄
*/
public
class
DeleteFileUtil
{
/** */
/**
* 刪除文件,可以是單個文件或文件夾
*
@param
fileName 待刪除的文件名
*
@return
文件刪除成功返回true,否則返回false
*/
public
static
boolean
delete(String fileName)
{
File file
=
new
File(fileName);
if
(
!
file.exists())
{
System.out.println(
"
刪除文件失敗:
"
+
fileName
+
"
文件不存在
"
);
return
false
;
}
else
{
if
(file.isFile())
{
return
deleteFile(fileName);
}
else
{
return
deleteDirectory(fileName);
}
}
}
/** */
/**
* 刪除單個文件
*
@param
fileName 被刪除文件的文件名
*
@return
單個文件刪除成功返回true,否則返回false
*/
public
static
boolean
deleteFile(String fileName)
{
File file
=
new
File(fileName);
if
(file.isFile()
&&
file.exists())
{
file.delete();
System.out.println(
"
刪除單個文件
"
+
fileName
+
"
成功!
"
);
return
true
;
}
else
{
System.out.println(
"
刪除單個文件
"
+
fileName
+
"
失敗!
"
);
return
false
;
}
}
/** */
/**
* 刪除目錄(文件夾)以及目錄下的文件
*
@param
dir 被刪除目錄的文件路徑
*
@return
目錄刪除成功返回true,否則返回false
*/
public
static
boolean
deleteDirectory(String dir)
{
//
如果dir不以文件分隔符結(jié)尾,自動添加文件分隔符
if
(
!
dir.endsWith(File.separator))
{
dir
=
dir
+
File.separator;
}
File dirFile
=
new
File(dir);
//
如果dir對應的文件不存在,或者不是一個目錄,則退出
if
(
!
dirFile.exists()
||
!
dirFile.isDirectory())
{
System.out.println(
"
刪除目錄失敗
"
+
dir
+
"
目錄不存在!
"
);
return
false
;
}
boolean
flag
=
true
;
//
刪除文件夾下的所有文件(包括子目錄)
File[] files
=
dirFile.listFiles();
for
(
int
i
=
0
;i
<
files.length;i
++
)
{
//
刪除子文件
if
(files[i].isFile())
{
flag
=
deleteFile(files[i].getAbsolutePath());
if
(
!
flag)
{
break
;
}
}
//
刪除子目錄
else
{
flag
=
deleteDirectory(files[i].getAbsolutePath());
if
(
!
flag)
{
break
;
}
}
}
if
(
!
flag)
{
System.out.println(
"
刪除目錄失敗
"
);
return
false
;
}
//
刪除當前目錄
if
(dirFile.delete())
{
System.out.println(
"
刪除目錄
"
+
dir
+
"
成功!
"
);
return
true
;
}
else
{
System.out.println(
"
刪除目錄
"
+
dir
+
"
失敗!
"
);
return
false
;
}
}
public
static
void
main(String[] args)
{
//
String fileName = "g:/temp/xwz.txt";
//
DeleteFileUtil.deleteFile(fileName);
String fileDir
=
"
D:\\廣告
"
;
//
DeleteFileUtil.deleteDirectory(fileDir);
DeleteFileUtil.deleteDirectory(fileDir);
}
}
posted on 2011-03-22 10:38
Mr.lu
閱讀(191)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © Mr.lu
<
2025年5月
>
日
一
二
三
四
五
六
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
導航
BlogJava
首頁
新隨筆
聯(lián)系
聚合
管理
統(tǒng)計
隨筆 - 29
文章 - 25
評論 - 26
引用 - 0
常用鏈接
我的隨筆
我的文章
我的評論
我的參與
最新評論
留言簿
(2)
給我留言
查看公開留言
查看私人留言
隨筆檔案
2016年1月 (1)
2015年12月 (2)
2015年4月 (1)
2015年3月 (1)
2014年12月 (1)
2013年12月 (1)
2013年8月 (7)
2013年5月 (1)
2013年4月 (4)
2013年3月 (4)
2012年11月 (1)
2012年10月 (4)
2012年9月 (1)
文章檔案
2016年2月 (1)
2014年12月 (1)
2014年5月 (1)
2014年4月 (1)
2013年4月 (1)
2013年3月 (1)
2012年9月 (5)
2012年6月 (1)
2011年6月 (2)
2011年4月 (5)
2011年3月 (6)
搜索
最新評論
1.?re: textarea中輸入換行、空格等,以正確的格式后臺存儲和前臺顯示
我了
個去
什么
--淡淡的
2.?re: textarea中輸入換行、空格等,以正確的格式后臺存儲和前臺顯示
法規(guī)和法規(guī)和法規(guī)和符合人體后對符合人體會讓他的返回任何人的融合和的個人各的人格的若干的負荷的任何
--的發(fā)貨的費
3.?re: JPA學習筆記
非常實用
--劉高潮
4.?re: textarea中輸入換行、空格等,以正確的格式后臺存儲和前臺顯示[未登錄]
啊啊啊啊 啊啊啊啊
啊啊啊啊
--1
5.?re: JPA學習筆記
評論內(nèi)容較長,點擊標題查看
--zuidaima
閱讀排行榜
1.?JPA學習筆記(20244)
2.?jQuery MiniUI學習(轉(zhuǎn)載)(11719)
3.?獲取八位UUID標識碼(3379)
4.?將私有的jar包導入到maven本地庫(1602)
5.?從url獲取圖片(1429)
評論排行榜
1.?JPA學習筆記(3)
2.?從url獲取圖片(0)
3.?webservice axis2學習(轉(zhuǎn)載地址)(0)
4.?jQuery MiniUI學習(轉(zhuǎn)載)(0)
5.?jquery ajax范例(0)
主站蜘蛛池模板:
武山县
|
鄂托克旗
|
龙泉市
|
崇明县
|
交城县
|
双桥区
|
栾川县
|
米林县
|
正安县
|
黔西县
|
武宣县
|
万年县
|
许昌县
|
乌海市
|
南木林县
|
新田县
|
静海县
|
奇台县
|
许昌县
|
栖霞市
|
崇明县
|
云梦县
|
大兴区
|
宁化县
|
紫阳县
|
金湖县
|
宾川县
|
榆社县
|
德兴市
|
崇州市
|
墨竹工卡县
|
元阳县
|
乌兰县
|
伊金霍洛旗
|
思南县
|
凤凰县
|
蒙山县
|
洛宁县
|
石门县
|
安阳市
|
民县
|