java將幾個文件打包 新建文件夾 刪除文件夾 将某文件夹下面的文件移动到另一个文件夹

package cn.shop.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;


public class TestPackages {


/**
* 将几个文件打包成一个zip

* @param list
*            要打包的文件的名称
* @param path
*            要打包的文件路径
* @param fileName
*            打包的包名
* @throws Exception
*/
public static void getZip(List list, String path, String fileName)
throws Exception {
byte[] buffer = new byte[1024];
String strZipName = fileName + ".zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(path
+ strZipName));
for (int j = 0; j < list.size(); j++) {
String name = list.get(j).toString();
FileInputStream fis = new FileInputStream(path + name);
out.putNextEntry(new ZipEntry(name));
int len;
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
out.close();
System.out.println("ok");
}


/**
* 将某文件夹下面的文件移动到另一个文件夹

* @param dir
*            要移动的文件夹名
* @param toFilePath
*            移动后文件存储的新文件夹名
*/
public static void getFile(File dir, String toFilePath) {
File[] files = dir.listFiles();
try {
for (int x = 0; x < files.length; x++) {
if (files[x].isDirectory()) {
getFile(files[x], toFilePath);
} else {
FileInputStream fis = new FileInputStream(files[x]
.getPath());
FileOutputStream fos = new FileOutputStream(toFilePath
+ "\\" + files[x].getName());
byte[] bt = new byte[2048];
int c;
while ((c = fis.read(bt)) != -1) {
try {
fos.write(bt, 0, c);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
fos.flush();
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}


/**
* 根据对应的日期格式和日期返回相对应的日期

* @param date
* @param format
* @return
*/
public static String formatDate(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}


public static void main(String[] args) {
// List<String> list = new ArrayList<String>();
// list.add("缺勤统计表(2012年10月).xls");
// list.add("考勤明细表(2012年10月).xls");
// try {
// getZip(list, "D:/data/考勤文档/当月/", "生成好的");
// } catch (Exception e) {
// e.printStackTrace();
// }
// 文件移动
// File file = new File("D:\\data\\考勤文档\\当月");
// getFile(file, "D:\\data\\考勤文档\\历史\\2012年10月");
// newFolder("D:\\data\\考勤文档\\历史\\2012年11月");
File file = new File("D:\\data\\考勤文档\\当月");
String statisM = formatDate(new Date(), "yyyy年MM月");
String toFilePath = "D:\\data\\考勤文档\\历史\\" + statisM;
getFile(file, toFilePath);
delAllFile("D:/data/考勤文档/当月");
}


// 新建一个文件夹
public static void newFolder(String folderPath) {
try {
String filePath = folderPath;
File myFilePath = new File(filePath);
if (!myFilePath.exists()) {
myFilePath.mkdir();
}
} catch (Exception e) {
System.out.println("新建文件夹操作出错");
e.printStackTrace();
}
}


// 删除文件夹
public static void delFolder(String folderPath) {
try {
String filePath = folderPath;
File delPath = new File(filePath);
delPath.delete();
} catch (Exception e) {
System.out.println("删除文件夹操作出错");
e.printStackTrace();
}
}


/**
* 删除牟文件夹下面所有文件

* @param path
* @return
*/
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
System.out.println(temp);
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);// 再删除空文件夹
flag = true;
}
}
return flag;
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值