Commit b9ebe93c by stylefeng

添加删除目录的方法

parent 0d414f63
package com.stylefeng.guns.core.util; 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.GunsException;
import com.stylefeng.guns.core.exception.GunsExceptionEnum;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -12,43 +12,62 @@ import java.nio.ByteBuffer; ...@@ -12,43 +12,62 @@ import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
public class FileUtil { public class FileUtil {
private static Logger log = LoggerFactory.getLogger(FileUtil.class);
/**
* NIO way
*/
public static byte[] toByteArray(String filename) {
File f = new File(filename); private static Logger log = LoggerFactory.getLogger(FileUtil.class);
if (!f.exists()) {
log.error("文件未找到!" + filename); /**
throw new GunsException(GunsExceptionEnum.FILE_NOT_FOUND); * NIO way
} */
FileChannel channel = null; public static byte[] toByteArray(String filename) {
FileInputStream fs = null;
try { File f = new File(filename);
fs = new FileInputStream(f); if (!f.exists()) {
channel = fs.getChannel(); log.error("文件未找到!" + filename);
ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size()); throw new GunsException(GunsExceptionEnum.FILE_NOT_FOUND);
while ((channel.read(byteBuffer)) > 0) { }
// do nothing FileChannel channel = null;
// System.out.println("reading"); FileInputStream fs = null;
} try {
return byteBuffer.array(); fs = new FileInputStream(f);
} catch (IOException e) { channel = fs.getChannel();
throw new GunsException(GunsExceptionEnum.FILE_READING_ERROR); ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
} finally { while ((channel.read(byteBuffer)) > 0) {
try { // do nothing
channel.close(); // System.out.println("reading");
} catch (IOException e) { }
throw new GunsException(GunsExceptionEnum.FILE_READING_ERROR); return byteBuffer.array();
} } catch (IOException e) {
try { throw new GunsException(GunsExceptionEnum.FILE_READING_ERROR);
fs.close(); } finally {
} catch (IOException e) { try {
throw new GunsException(GunsExceptionEnum.FILE_READING_ERROR); 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment