Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
guns-vip
Overview
Overview
Details
Activity
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chenjunxiong
guns-vip
Commits
b9ebe93c
Commit
b9ebe93c
authored
Oct 30, 2017
by
stylefeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加删除目录的方法
parent
0d414f63
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
39 deletions
+59
-39
guns-core/src/main/java/com/stylefeng/guns/core/util/FileUtil.java
+59
-39
No files found.
guns-core/src/main/java/com/stylefeng/guns/core/util/FileUtil.java
View file @
b9ebe93c
package
com
.
stylefeng
.
guns
.
core
.
util
;
import
com.stylefeng.guns.core.exception.GunsExceptionEnum
;
import
com.stylefeng.guns.core.exception.GunsException
;
import
com.stylefeng.guns.core.exception.GunsExceptionEnum
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
...
...
@@ -12,43 +12,62 @@ import java.nio.ByteBuffer;
import
java.nio.channels.FileChannel
;
public
class
FileUtil
{
private
static
Logger
log
=
LoggerFactory
.
getLogger
(
FileUtil
.
class
);
/**
* NIO way
*/
public
static
byte
[]
toByteArray
(
String
filename
)
{
File
f
=
new
File
(
filename
);
if
(!
f
.
exists
())
{
log
.
error
(
"文件未找到!"
+
filename
);
throw
new
GunsException
(
GunsExceptionEnum
.
FILE_NOT_FOUND
);
}
FileChannel
channel
=
null
;
FileInputStream
fs
=
null
;
try
{
fs
=
new
FileInputStream
(
f
);
channel
=
fs
.
getChannel
();
ByteBuffer
byteBuffer
=
ByteBuffer
.
allocate
((
int
)
channel
.
size
());
while
((
channel
.
read
(
byteBuffer
))
>
0
)
{
// do nothing
// System.out.println("reading");
}
return
byteBuffer
.
array
();
}
catch
(
IOException
e
)
{
throw
new
GunsException
(
GunsExceptionEnum
.
FILE_READING_ERROR
);
}
finally
{
try
{
channel
.
close
();
}
catch
(
IOException
e
)
{
throw
new
GunsException
(
GunsExceptionEnum
.
FILE_READING_ERROR
);
}
try
{
fs
.
close
();
}
catch
(
IOException
e
)
{
throw
new
GunsException
(
GunsExceptionEnum
.
FILE_READING_ERROR
);
}
}
}
private
static
Logger
log
=
LoggerFactory
.
getLogger
(
FileUtil
.
class
);
/**
* NIO way
*/
public
static
byte
[]
toByteArray
(
String
filename
)
{
File
f
=
new
File
(
filename
);
if
(!
f
.
exists
())
{
log
.
error
(
"文件未找到!"
+
filename
);
throw
new
GunsException
(
GunsExceptionEnum
.
FILE_NOT_FOUND
);
}
FileChannel
channel
=
null
;
FileInputStream
fs
=
null
;
try
{
fs
=
new
FileInputStream
(
f
);
channel
=
fs
.
getChannel
();
ByteBuffer
byteBuffer
=
ByteBuffer
.
allocate
((
int
)
channel
.
size
());
while
((
channel
.
read
(
byteBuffer
))
>
0
)
{
// do nothing
// System.out.println("reading");
}
return
byteBuffer
.
array
();
}
catch
(
IOException
e
)
{
throw
new
GunsException
(
GunsExceptionEnum
.
FILE_READING_ERROR
);
}
finally
{
try
{
channel
.
close
();
}
catch
(
IOException
e
)
{
throw
new
GunsException
(
GunsExceptionEnum
.
FILE_READING_ERROR
);
}
try
{
fs
.
close
();
}
catch
(
IOException
e
)
{
throw
new
GunsException
(
GunsExceptionEnum
.
FILE_READING_ERROR
);
}
}
}
/**
* 删除目录
*
* @author fengshuonan
* @Date 2017/10/30 下午4:15
*/
public
static
boolean
deleteDir
(
File
dir
)
{
if
(
dir
.
isDirectory
())
{
String
[]
children
=
dir
.
list
();
for
(
int
i
=
0
;
i
<
children
.
length
;
i
++)
{
boolean
success
=
deleteDir
(
new
File
(
dir
,
children
[
i
]));
if
(!
success
)
{
return
false
;
}
}
}
return
dir
.
delete
();
}
}
\ No newline at end of file
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