文件的复制和删除


/**
* 删除文件,通过文件的类型
* @param filePath
* @param type
*/

public static void deleteType(String filePath,String type){
File file = new File(filePath);
if(!file.isDirectory())
return;
String[] list = file.list();
if(list==null)
return;
for(int i=0;i<list.length;i++){
File f = new File(file,list[i]);
String panth = f.getPath();
if(f.isDirectory()){
deleteType(panth,type);
}
if(panth.endsWith(type)){
System.out.println(f.getFreeSpace()+","+panth);
//f.delete();
}
}
file.delete();
}


/**
* 复制文件
* @param fromFilepath
* @param toFilepath
*/
public static void copyFile(File fromFile,File tofile){
if(tofile!=null&&tofile.isDirectory()){
tofile = new File(tofile,fromFile!=null?fromFile.getName():"");
}
InputStream inputs =null;
OutputStream output =null;
try {
inputs = new FileInputStream(fromFile);
output = new FileOutputStream(tofile);
byte[] b = new byte[1024];
int len = inputs.read(b);
while(len!=-1){
output.write(b, 0, len);
len = inputs.read(b);
}
//System.out.println("----------copy successful-------------");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

/**
* 复制文件夹
* @param fromFilepath
* @param toFilepath
*/
public static void copyDir(String fromFilepath,String toFilepath){
File fromFile = new File(fromFilepath);
File toFile = new File(toFilepath);
if(fromFile.isDirectory()&&flag){
toFilepath = toFilepath+"/"+fromFile.getName();
toFile = new File(toFilepath);
if(!toFile.exists())
toFile.mkdirs();
flag = false;
}

String[] list = fromFile.list();
if(list==null)
return;
for(int i=0;i<list.length;i++){
File file = new File(fromFile,list[i]);


if(file!=null&&file.isDirectory()){
toFile = new File(toFilepath,list[i]);
if(!toFile.exists())
toFile.mkdirs();
System.out.println("--------"+file.getPath()+"-------------i="+i+"----->"+list[i]);
System.out.println("++++++++"+toFile.getPath()+"+++++++++++++");
copyDir(file.getPath(),toFile.getPath());

}else
copyFile(file, toFile);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值