自己写的一个基于nio的文件夹扫描和文件转移删除

话不多说,直接上代码
是我用来读取ftp上传路径下面的文件并且转移和删除的
小文件很快,但是大文件就很满了,主要原因就是在缓冲数组扩容的地方的瓶颈
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FtpIn {

public String path="C:"+File.separator+"ftpserver-1.0.4"+File.separator+"res"+File.separator+"home";
/**
* 递归获取指定文件夹下面的所有文件内容,包括子目录下面的文件
* @param delPath
* @throws Exception
*/
public void doGet(String delPath){
File file =new File(delPath);
if(file.isDirectory()){
String[] fileNames=file.list();
for(int i=0;i<fileNames.length;i++){
String fileName=delPath+File.separator+fileNames[i];
File tempFile=new File(fileName);
if(tempFile.isDirectory()){
//如果是个文件夹就递归继续读取
doGet(fileName);
}else{
//System.out.println("删除文件:"+tempFile.getName());
//tempFile.delete();
byte[] req = getFileBytes(tempFile);
System.out.println("读取到文件名:"+tempFile.getName()+" 大小为:"+req.length);
if(req!=null&&req.length>0){
writeFileByBytes(req,"d:"+File.separator+tempFile.getName());
}
//System.out.println("文件内容为:\n"+new String(req));
}
}
if(path!=file.getAbsolutePath()){
System.out.println("删除文件夹:"+file.getName());
//file.delete();
}
}else{
System.out.println("删除文件:"+file.getName());
//file.delete();
byte[] req = getFileBytes(file);
System.out.println("读取到文件名:"+file.getName()+" 大小为:"+req.length);
if(req!=null&&req.length>0){
writeFileByBytes(req,"d:"+File.separator+file.getName());
}
//System.out.println("文件内容为:\n"+new String(req));
}
}
/**
* 把指定byte数组内的内容全写到指定文件中
* @param b
* @param path
* @return boolean 是否成功
*/
public boolean writeFileByBytes(byte[] b,String path){
FileOutputStream fo=null;
FileChannel fc=null;
boolean flag=false;
ByteBuffer buffer = ByteBuffer.wrap(b);
try {
fo=new FileOutputStream(path);
if(fo==null) return flag;
fc=fo.getChannel();
int i= fc.write(buffer);
System.out.println("写入了:"+i+"个字节");
//如果写的字节数和传入的字节数一致就表示写成功
if(i==b.length) flag=true;
if(fc!=null) fc.close();
if(fo!=null) fo.close();
} catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return flag;
}
/**
* 获得一个文件的byte[]内容
* @param file
* @return
*/
public byte[] getFileBytes(File file){
FileInputStream fi=null;
FileChannel fc=null;
ByteBuffer buffer = ByteBuffer.allocate(1024);
int off = 0;
int r = 0;
byte[] data = new byte[1024 * 10];
try {
fi=new FileInputStream(file);
if(fi==null) return null;
fc=fi.getChannel();
while ( true ) {
buffer.clear();
r = fc.read(buffer);
if (r == -1) break;
if ( (off + r) > data.length) {
data = grow(data, 1024 * 10);
}
byte[] buf = buffer.array();
System.arraycopy(buf, 0, data, off, r);
off += r;
}
if(fc!=null)fc.close();
if(fi!=null) fi.close();
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
byte[] req = new byte[off];
System.arraycopy(data, 0, req, 0, off);

return req;
}
/**
* 删除一个文件夹下面所有文件(包括子文件价下面的)
* @param delPath
* @throws Exception
*/
public void doDelete(String delPath){
File file =new File(delPath);
if(file.isDirectory()){
String[] fileNames=file.list();
for(int i=0;i<fileNames.length;i++){
String fileName=delPath+File.separator+fileNames[i];
File tempFile=new File(fileName);
if(tempFile.isDirectory()){
//递归
doDelete(fileName);
}else{
System.out.println("删除文件:"+tempFile.getName());
tempFile.delete();
}
}
if(path!=file.getAbsolutePath()){
System.out.println("删除文件夹:"+file.getName());
//file.delete();
}
}else{
System.out.println("删除文件:"+file.getName());
//file.delete();
}
}
/**
* 数组扩容
* @param src byte[] 源数组数据
* @param size int 扩容的增加量
* @return byte[] 扩容后的数组
*/
public static byte[] grow(byte[] src, int size) {
byte[] tmp = new byte[src.length + size];
System.arraycopy(src, 0, tmp, 0, src.length);
return tmp;
}
public static void main(String... args){
FtpIn fi=new FtpIn();
fi.doGet(fi.path);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值