java FileUtil(文件相关工具类)

574 篇文章 4 订阅

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.List;

/**

  • 功能:对java中的File进行2次封装

/
public class FileUtil {
/
*
* 根据路径创建指定的文件夹
*
* @param path
* 文件夹路径
*/
public static void touchPath(String path) {
try {
File filePath = new File(path);
if (!filePath.exists()) {
filePath.mkdirs();
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
 * 文件备份操作,备份后的路径是:path+/当前时间(yyyy-dd-MM-HH-mm-ss)
 * 
 * @param path
 *            要备份的路径
 * @return 已经备份的路径
 */
public static String backup(String path) {
	final String dstPath = path
			+ "."
			+ TimeUtil.toString(System.currentTimeMillis(),
					"yyyy-dd-MM-HH-mm-ss");
	backup(path, dstPath);
	return dstPath;
}

/**
 * 文件备份操作
 * 
 * @param srcPath
 *            文件元路径
 * @param dstPath
 *            备份目标路径
 */
public static void backup(final String srcPath, final String dstPath) {
	try {
		File srcFile = new File(srcPath);
		File dstFile = new File(dstPath);
		org.apache.avalon.excalibur.io.FileUtil.copyFile(srcFile, dstFile);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

/**
 * 文件拷贝
 * 
 * @param srcFile
 *            要拷贝的文件
 * @param dstPath
 *            拷贝的路径
 * @return =true 拷贝成功 =false 拷贝失败
 */
public static boolean copy(File srcFile, final String dstPath) {
	try {
		File dstFile = new File(dstPath);
		org.apache.avalon.excalibur.io.FileUtil.copyFile(srcFile, dstFile);
		return true;
	} catch (Exception e) {
		e.printStackTrace();
		return false;
	}
}

/**
 * 输入流拷贝文件
 * 
 * @param src
 *            文件输入流
 * @param dstPath
 *            文件输出路径
 * @return =true 拷贝成功 =false 拷贝失败
 */
public static boolean copy(InputStream src, final String dstPath) {
	try {
		File dstFile = new File(dstPath);
		byte[] buffer = new byte[64 * 1024];
		int length = 0;
		OutputStream os = new FileOutputStream(dstFile);

		try {
			while ((length = src.read(buffer, 0, buffer.length)) != -1) {
				os.write(buffer, 0, length);
			}
		} finally {
			close(os);
		}
		return true;
	} catch (Exception e) {
		e.printStackTrace();
		return false;
	}
}

/**
 * 文件添加后缀
 * 
 * @param path
 *            文件(带路径)
 * @param suffix
 *            后缀名称
 */
public static void rename(String path, String suffix) {
	try {
		File file = new File(path);
		File dstFile = new File(path + "." + suffix);
		file.renameTo(dstFile);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

/**
 * 指定路径下的文件列表
 * 
 * @param fileInfo
 *            文件信息对象
 * @param path
 *            文件所在路径
 * @return 指定路径下的文件列表
 */
public static String[] toFileList(FileInfo fileInfo, String path) {
	String[] listfilenames;
	File file = new File(path);
	listfilenames = file.list();
	if (null == listfilenames) {
		fileInfo.setTotalCount(0);
		return new String[] {};
	}

	fileInfo.setTotalCount(listfilenames.length);
	return listfilenames;
}

/**
 * 
 *获取指定文件夹下的所有文件,包括子文件夹的文件名称.<br>
 *工程名:cctccati<br>
 *包名:com.cctchina.util.type<br>
 *方法名:toFileList方法.<br>
 * 
 *@author:pu_xiaolong@sina.com
 *@since :1.0:2009-6-26
 *@param strPath
 *            :指定文件夹路径
 *@param filelist
 *            :获取的文件集合
 */
public static void toFileList(String strPath, List<String> filelist) {
	File dir = new File(strPath);
	File[] files = dir.listFiles();
	if (files == null) {
		return;
	}
	for (int i = 0; i < files.length; i++) {
		if (files[i].isDirectory()) {
			toFileList(files[i].getAbsolutePath(), filelist);
		} else {
			// filelist.add(files[i].getAbsolutePath());
			filelist.add(files[i].getName());
		}
	}

}

/**
 * 关闭输入流
 * 
 * @param stream
 *            输入流
 */
public static void close(InputStream stream) {
	try {
		stream.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}

/**
 * 关闭输出流
 * 
 * @param stream
 *            输出流
 */
public static void close(OutputStream stream) {
	try {
		stream.flush();
		stream.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}

/**
 * 关闭读入文件流
 * 
 * @param reader
 *            读入文件流
 */
public static void close(Reader reader) {
	try {
		reader.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}

/**
 * 关闭寫入文件流
 * 
 * @param writer
 *            寫入文件流
 */
public static void close(Writer writer) {
	try {
		writer.flush();
		writer.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}

/**
 * 删除指定路径下的文件
 * 
 * @param dirPath
 *            指定路径
 * @throws java.lang.Exception
 *             删除失败错误
 */
public static void deleteAllSubFiles(String dirPath) throws Exception {
	try {
		File dir = new File(dirPath);
		if (dir.isDirectory()) {
			File[] subFiles = dir.listFiles();
			for (int i = 0; i < subFiles.length; i++) {
				File subFile = subFiles[i];
				try {
					subFile.delete();
				} catch (Exception e) {
					throw e;
				}
			}
		}
	} catch (Exception e) {
		throw e;
	}
}

/**
 * 文件是否存在错误
 * 
 * @param filePathname
 *            指定的文件
 * @return =true 文件存在 false 文件不存在
 */
public static boolean isFileExisted(String filePathname) {
	boolean existed;
	try {
		File f = new File(filePathname);
		existed = f.isFile();
	} catch (Exception e) {
		existed = false;
	}
	return existed;
}

/**
 * 去掉文件的路径,只留下文件名
 * 
 * @param pathname
 *            带路径的文件名
 * @return 文件名称
 */
public static String removePath(String pathname) {
	String fname = pathname;
	int index = pathname.lastIndexOf(File.separator);
	if (index >= 0) {
		fname = pathname.substring(index + 1);
	}
	return fname;
}

/**
 * 去掉文件名而只保留文件路径
 * 
 * @param pathname
 *            带路径的文件名
 * @return 文件路径
 */
public static String removeFileName(String pathname) {
	String fname = pathname;
	int index = pathname.lastIndexOf(File.separator);
	if (index >= 0) {
		fname = pathname.substring(0, index);
	}
	return fname;
}

/**
 *根据路径删除指定的目录或文件,无论存在与否<br>
 *工程名:cctcpsp<br>
 *包名:com.cctchina.psp.action.knowledgebase.knowledgetype<br>
 *方法名:DeleteFolder方法.<br>
 * 
 *@author:jiangwenqi
 *@since :1.0:2009-11-5
 *@param sPath
 *            要删除的目录或文件
 *@return 删除成功返回 true,否则返回 false。
 */
public static boolean DeleteFolder(String sPath) {
	boolean flag = false;
	File file = new File(sPath);
	// 判断目录或文件是否存在
	if (!file.exists()) { // 不存在返回 false
		return flag;
	} else {
		// 判断是否为文件
		if (file.isFile()) { // 为文件时调用删除文件方法
			return deleteFile(sPath);
		} else { // 为目录时调用删除目录方法
			return deleteDirectory(sPath,true);
		}
	}
}

/**
 *删除单个文件.<br>
 *工程名:cctcpsp<br>
 *包名:com.cctchina.psp.action.knowledgebase.knowledgetype<br>
 *方法名:deleteFile方法.<br>
 * 
 *@author:jiangwenqi
 *@since :1.0:2009-11-5
 *@param sPath
 *            被删除文件的文件名
 *@return 单个文件删除成功返回true,否则返回false
 */
public static boolean deleteFile(String sPath) {
	boolean flag = false;
	File file = new File(sPath);
	// 路径为文件且不为空则进行删除
	if (file.isFile() && file.exists()) {
		file.delete();
		flag = true;
	}
	return flag;
}

/**
 * 删除目录(文件夹)以及目录下的文件 工程名:cctcpsp<br>
 *包名:com.cctchina.psp.action.knowledgebase.knowledgetype<br>
 *方法名:deleteDirectory方法.<br>
 * 
 *@author:jiangwenqi
 *@since :1.0:2009-11-5
 *@param sPath
 *            被删除目录的文件路径
 *@return 目录删除成功返回true,否则返回false
 */
public static boolean deleteDirectory(String sPath,boolean selfFlag) {
	// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
	if (!sPath.endsWith(File.separator)) {
		sPath = sPath + File.separator;
	}
	File dirFile = new File(sPath);
	// 如果dir对应的文件不存在,或者不是一个目录,则退出
	if (!dirFile.exists() || !dirFile.isDirectory()) {
		return false;
	}
	boolean flag = true;
	// 删除文件夹下的所有文件(包括子目录)
	File[] files = dirFile.listFiles();
	for (int i = 0; i < files.length; i++) {
		// 删除子文件
		if (files[i].isFile()) {
			flag = deleteFile(files[i].getAbsolutePath());
			if (!flag)
				break;
		} // 删除子目录
		else {
			flag = deleteDirectory(files[i].getAbsolutePath(),true);
			if (!flag)
				break;
		}
	}
	if (!flag)
		return false;
	if(selfFlag){// 删除当前目录
	    if (dirFile.delete()) {
            return true;
        } else {
            return false;
        }  
	} else{
	    return true;
	} 
}   

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值