java的File的工具类

包括

void listDirectory(File dir):列出指定目录下(包括其子目录)的所有文件,打印在控制台

void deleteDirectory(File dir):删除指定目录及其包含的所有内容

List<File> listFile(File dir,final String[] extensions, boolean recursive): 列出指定目录下指定扩展名的所有文件

List<File> listFile(File dir, final String filename, boolean recursive):列出文件名为指定文件名的文件

List<File> listFile(File dir, FileFilter ff, boolean recursive):列出指定文件过滤器的文件

void copyFile(File srcFile, File destFile):复制文件

void copyDirectory(File srcDir, File destDir):复制目录,以及下面的文件

 

public class FileUtil {

	/**
	 * 
	 * 列出指定目录下(包括其子目录)的所有文件
	 * 
	 */

	public static void listDirectory(File dir)

	throws IOException {

		if (!dir.exists()) {

			throw new IllegalArgumentException("目录:" + dir

			+ "不存在");

		}

		if (!dir.isDirectory()) {

			throw new IllegalArgumentException(dir + "不是目录");

		}

		File[] subs = dir.listFiles();

		if (subs != null && subs.length > 0) {

			for (File sub : subs) {

				if (sub.isDirectory()) {

					listDirectory(sub);

				} else {

					System.out.println(sub);

				}

			}

		}

	}

	/**
	 * 
	 * 删除指定目录及其包含的所有内容
	 * 
	 * 
	 * */

	public static void deleteDirectory(File dir)

	throws IOException {

		if (!dir.exists()) {

			throw new IllegalArgumentException("目录:" + dir +

			"不存在");

		}

		if (!dir.isDirectory()) {

			throw new IllegalArgumentException(dir + "不是目录");

		}

		File[] subs = dir.listFiles();

		if (subs != null && subs.length > 0) {

			for (File sub : subs) {

				if (sub.isDirectory()) {

					deleteDirectory(sub);

				} else {

					System.out.println(sub);

					if (!sub.delete()) {

						throw new IOException("无法删除文件:" + sub);

					}

				}

			}

		}

		System.out.println(dir);

		if (!dir.delete()) {

			throw new IOException("无法删除目录:" + dir);

		}

	}

	/**
	 * 
	 * 列出指定目录下指定扩展名的所有文件
	 * 
	 */

	public static List<File> listFile(File dir,

	final String[] extensions, boolean recursive) {

		if (!dir.exists()) {

			throw new IllegalArgumentException("目录:" + dir +

			"不存在");

		}

		if (!dir.isDirectory()) {

			throw new IllegalArgumentException(dir + "不是目录");

		}

		FileFilter ff = null;

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

			ff = new FileFilter() {

				@Override
				public boolean accept(File pathname) {

					return true;

				}

			};

		} else {

			ff = new FileFilter() {

				@Override
				public boolean accept(File pathname) {

					if (pathname.isDirectory())

						return true;

					String name = pathname.getName();

					for (String ext : extensions) {

						if (name.endsWith(ext)) {

							return true;

						}

					}

					return false;

				}

			};

		}

		return listFile(dir, ff, recursive);

	}

	/**
	 * 
	 * 列出文件名为指定文件名的文件
	 * 
	 * */

	public static List<File> listFile(File dir,

	final String filename, boolean recursive) {

		if (!dir.exists()) {

			throw new IllegalArgumentException("目录:" + dir

			+ "不存在");

		}

		if (!dir.isDirectory()) {

			throw new IllegalArgumentException(dir + "不是目录");

		}

		FileFilter ff = null;

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

			ff = new FileFilter() {

				@Override
				public boolean accept(File pathname) {

					return true;

				}

			};

		} else {

			ff = new FileFilter() {

				@Override
				public boolean accept(File pathname) {

					if (pathname.isDirectory())

						return true;

					String name = pathname.getName();

					if (name.indexOf(filename) != -1)

						return true;

					else

						return false;

				}

			};

		}

		return listFile(dir, ff, recursive);

	}

	private static List<File> listFile(File dir, FileFilter ff,

	boolean recursive) {

		List<File> list = new ArrayList<File>();

		File[] subs = dir.listFiles(ff);

		if (subs != null && subs.length > 0) {

			for (File sub : subs) {

				if (sub.isFile()) {

					list.add(sub);

				} else if (recursive) {

					list.addAll(listFile(sub, ff, true));

				}

			}

		}

		return list;

	}

	/***
	 * 复制文件
	 * 
	 * @param srcFile
	 * @param destFile
	 * @throws IOException
	 */
	public static void copyFile(File srcFile, File destFile)

	throws IOException {

		if (!srcFile.exists()) {

			throw new IllegalArgumentException("文件:" + srcFile

			+ "不存在");

		}

		if (!srcFile.isFile()) {

			throw new IllegalArgumentException(srcFile +

			"不是文件");

		}

		RandomAccessFile srcRaf = new RandomAccessFile(srcFile,

		"r");

		if (!destFile.exists()) {

			if (!destFile.createNewFile())

				throw new IOException("无法创建文件:" + destFile);

		}

		RandomAccessFile destRaf = new RandomAccessFile(

		destFile, "rw");

		byte[] buffer = new byte[8 * 1024];

		int len = -1;

		while ((len = srcRaf.read(buffer)) != -1) {

			destRaf.write(buffer, 0, len);

		}

		srcRaf.close();

		destRaf.close();

	}

	/***
	 * 复制文件夹
	 * 
	 * @param srcDir
	 * @param destDir
	 * @throws IOException
	 */

	public static void copyDirectory(File srcDir, File destDir)

	throws IOException {

		if (!srcDir.exists()) {

			throw new IllegalArgumentException("文件:" +

			srcDir + "不存在");

		}

		if (!srcDir.isDirectory()) {

			throw new IllegalArgumentException(srcDir +

			"不是目录");

		}

		if (!destDir.exists()) {

			if (!destDir.mkdir())

				throw new IOException("无法创建目录:" + destDir);

		}

		File[] subs = srcDir.listFiles();

		if (subs != null && subs.length > 0) {

			for (File sub : subs) {

				if (sub.isFile()) {

					copyFile(sub, new File(destDir,

					sub.getName()));

				} else if (sub.isDirectory()) {

					copyDirectory(sub, new File(destDir,

					sub.getName()));

				}

			}

		}

	}

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /** * Read content from local file. FIXME How to judge UTF-8 and GBK, the * correct code should be: FileReader fr = new FileReader(new * InputStreamReader(fileName, "ENCODING")); Might let the user select the * encoding would be a better idea. While reading UTF-8 files, the content * is bad when saved out. * * @param fileName - * local file name to read * @return * @throws Exception */ public static String readFileAsString(String fileName) throws Exception { String content = new String(readFileBinary(fileName)); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws Exception { String content = new String(readFileBinary(fileName), encoding); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(InputStream in) throws Exception { String content = new String(readFileBinary(in)); return content; } /** * Read content from local file to binary byte array. * * @param fileName - * local file name to read * @return * @throws Exception */ public static byte[] readFileBinary(String fileName) throws Exception { FileInputStream fin = new FileInputStream(fileName); return readFileBinary(fin); } /** * 从输入流读取数据为二进制字节数组. * @param streamIn * @return * @throws IOException */ public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int len; while ((len

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值