Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
2b6017cc
Commit
2b6017cc
authored
Apr 22, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
压缩处理
parent
f89af689
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
79 additions
and
0 deletions
+79
-0
FileHelper.java
...src/main/java/com/yanzuoguang/util/helper/FileHelper.java
+79
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/FileHelper.java
View file @
2b6017cc
...
...
@@ -105,4 +105,83 @@ public class FileHelper {
throw
new
CodeException
(
e
);
}
}
/**
* 根据路径删除指定的目录或文件,无论存在与否
*
* @param sPath 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
*/
public
static
boolean
deleteFolder
(
String
sPath
)
{
boolean
flag
=
false
;
File
file
=
new
File
(
sPath
);
// 判断目录或文件是否存在
if
(!
file
.
exists
())
{
// 不存在返回 false
return
flag
;
}
else
{
// 判断是否为文件
if
(
file
.
isFile
())
{
// 为文件时调用删除文件方法
return
deleteFile
(
sPath
);
}
else
{
// 为目录时调用删除目录方法
return
deleteDirectory
(
sPath
);
}
}
}
/**
* 删除单个文件
*
* @param sPath 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public
static
boolean
deleteFile
(
String
sPath
)
{
boolean
flag
=
false
;
File
file
=
new
File
(
sPath
);
// 路径为文件且不为空则进行删除
if
(
file
.
isFile
()
&&
file
.
exists
())
{
file
.
delete
();
flag
=
true
;
}
return
flag
;
}
/**
* 删除目录(文件夹)以及目录下的文件
*
* @param sPath 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public
static
boolean
deleteDirectory
(
String
sPath
)
{
//如果sPath不以文件分隔符结尾,自动添加文件分隔符
if
(!
sPath
.
endsWith
(
File
.
separator
))
{
sPath
=
sPath
+
File
.
separator
;
}
File
dirFile
=
new
File
(
sPath
);
//如果dir对应的文件不存在,或者不是一个目录,则退出
if
(!
dirFile
.
exists
()
||
!
dirFile
.
isDirectory
())
{
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
)
return
false
;
//删除当前目录
if
(
dirFile
.
delete
())
{
return
true
;
}
else
{
return
false
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment