java file 工具_Java文件操作工具

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

/**

*

* @Title : FileUtils

* @File Name : FileUtils.java

* @Description : 文件操作工具类

* @Date : 2014年8月10日

* @author : 王鸿运

* @version : 1.0

* @Others :

* @History 1.

*          Date : 

*          Author : 

*          Modification: 

*/

public class FileUtils {

public static final String FILE_SEP = System.getProperty("file.separator");

/**

*

* @Function  :  moveFile

* @Desc  :  移动目录或者文件

* @Author  : 王鸿运

* @param srcFilePath  源文件(目录)路径

* @param destFilePath  目的文件(目录)路径

* @return  true:操作成功,false:操作失败

*/

public static boolean moveFile(String srcFilePath, String destFilePath) {

File srcFile = new File(srcFilePath);

if (!srcFile.exists()) {

return false;

}

if (srcFile.isDirectory()) {

File[] subFiles = srcFile.listFiles();

for (File file : subFiles) {

moveFile(file.getAbsolutePath(), destFilePath +FILE_SEP+ file.getName());

}

}

File destFile = new File(destFilePath);

File destParentFile = destFile.getParentFile();

if (!destParentFile.exists()) {

destParentFile.mkdirs();

}

srcFile.renameTo(destFile);

return true;

}

/**

* @Function  :  moveChildren

* @Desc  :  移动指定目录下所有的子目录和文件到目的目录

* @Author  : 王鸿运

* @param srcFilePath  源目录路径

* @param destFilePath  目的路径

* @return  true:操作成功,false:操作失败

*/

public static boolean moveAllChildren(String srcFilePath, String destFilePath) {

File srcDir = new File(srcFilePath);

if (!srcDir.exists() || !srcDir.isDirectory()) {

return false;

}

File destDir = new File(destFilePath);

if (destDir.exists()) {

if ( destDir.isFile()) {

destDir.delete();

destDir = new File(destFilePath);

destDir.mkdir();

}

} else {

destDir.mkdirs();

}

File[] children = srcDir.listFiles();

for (File file : children) {

moveFile(file.getAbsolutePath(), destFilePath);

}

return true;

}

/**

*

* @Function  :  moveFiles

* @Desc  :  将指定文件移动到目的目录

* @Author  : 王鸿运

* @param srcFiles 源文件,支持多个路径参数

* @param destDirPath 目的目录

* @return true:操作成功,false:操作失败

*/

public static boolean moveFiles(String destDirPath, String ... srcFiles){

if (srcFiles == null || srcFiles.length == 0) {

return false;

}

File destDir = new File(destDirPath);

boolean destExists = true;

if (!destDir.exists()) {

destExists = destDir.mkdirs();

} else if(destDir.isFile()){

return false;

}

if (!destExists) {

return false;

}

for (String srcFilePath : srcFiles) {

File srcFile = new File(srcFilePath);

if (srcFile.exists() && srcFile.isFile()) {

srcFile.renameTo(new File(destDirPath + FILE_SEP + srcFile.getName()));

}

}

return true;

}

/**

*

* @Function : deleteSelfAndAllChildren

* @Desc : 刪除指定目录和其下所有子目录和文件,如果指定的是文件则直接删除

* @Author : 王鸿运

* @param dirPath 源目录或文件路径

* @return true:删除成功,false:删除失败

*/

public static boolean deleteSelfAndAllChildren(String dirPath) {

File srcFile = new File(dirPath);

if (!srcFile.exists()) {

return false;

}

if (srcFile.isFile()) {

srcFile.delete();

} else {

File[] children = srcFile.listFiles();

if (children.length == 0) {

srcFile.delete();

} else {

for (File file : children) {//先删除子文件和目录

deleteSelfAndAllChildren(file.getAbsolutePath());

}

//再删除本身

srcFile.delete();

}

}

return true;

}

/**

*

* @Function  :  deleteAllChildren

* @Desc  :  删除一个目录下所有的子目录和文件

* @Author  : 王鸿运

* @param dirPath

* @return true:删除成功,false:删除失败

*/

public static boolean deleteAllChildren(String dirPath){

File dir = new File(dirPath);

if (!dir.exists()) {

return false;

}

File[] children = dir.listFiles();

for (File file : children) {

deleteSelfAndAllChildren(file.getAbsolutePath());

}

return true;

}

/**

*

* @Function  :  unzipFile

* @Desc  :  解压指定的zip压缩文件到指定的目的目录

* @Author  : 王鸿运

* @param zipFilePath

* @param destDirPath

* @return

*/

@SuppressWarnings("unchecked")

public static boolean unzipFile(String zipFilePath, String destDirPath){

File srcFile = new File(zipFilePath);

if (!srcFile.exists() || srcFile.isDirectory()) {

return false;

}

File destDir = new File(destDirPath);

if (!destDir.exists()) {

destDir.mkdirs();

} else if (destDir.isFile()) {

return false;

}

ZipFile zipFile = null;

try {

zipFile = new ZipFile(srcFile);

Enumeration zipEntries = (Enumeration) zipFile.entries();

while(zipEntries.hasMoreElements()){

ZipEntry zipEntry = zipEntries.nextElement();

if (zipEntry.isDirectory()) {

String entryName = zipEntry.getName();

File tmpFile = new File(destDirPath + FILE_SEP + entryName);

if (!tmpFile.exists()) {

tmpFile.mkdirs();

}

} else {

System.err.println(zipEntry.getName());

InputStream input = zipFile.getInputStream(zipEntry);

String entryName = zipEntry.getName();

OutputStream output = new FileOutputStream(new File(destDirPath + FILE_SEP + entryName));

streamCopy(input, output);

}

}

} catch (IOException e) {

e.printStackTrace();

return false;

}

return true;

}

/**

* @Function  :  streamCopy

* @Desc  :  流复制

* @Author  : 王鸿运

* @param input  InputStream 输入流

* @param output OutputStream 输出流

* @return long 流长度

* @throws IOException

*/

public static long streamCopy(final InputStream input, final OutputStream output) throws IOException {

final byte[] buffer = new byte[8192];

int n = 0;

long count=0;

while (-1 != (n = input.read(buffer))) {

output.write(buffer, 0, n);

count += n;

}

return count;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值