/**
* 拷贝文件
*/
public static void copyFile(File srcFile, File targetFile) throws IOException {
FileInputStream inputStream = new FileInputStream(srcFile);
FileOutputStream outputStream = new FileOutputStream(targetFile);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, len);
bufferedOutputStream.flush();
}
bufferedOutputStream.close();
inputStream.close();
outputStream.close();
}
/**
* 删除文件
*/
public static void deleteFile(File srcFile) {
srcFile.delete();
}
/**
* 剪切文件
*/
public static void cutFile(File srcFile, File targetFile) throws IOException {
copyFile(srcFile, targetFile);
deleteFile(srcFile);
}
/**
* 压缩文件
*/
public static void zipFiles(List<File> srcfile, File zipfile) {
byte[] buf = new byte[1024];
try {
//ZipOutputStream类:完成文件或文件夹的压缩
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
for (File file : srcfile) {
FileInputStream in = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file.getName()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
log.info("压缩完成.");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除文件夹
*/
public static boolean deleteDir(File dirFile) {
// 如果dir对应的文件不存在,则退出
if (!dirFile.exists()) {
return false;
}
if (dirFile.isFile()) {
return dirFile.delete();
} else {
for (File file : dirFile.listFiles()) {
deleteDir(file);
}
}
return dirFile.delete();
}
标签:文件,dirFile,srcFile,len,static,File,操作,new
From: https://www.cnblogs.com/tian0926/p/17280085.html