java.file文件合并

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

public class FileUtil {
	public static boolean mergeFilesToOneFile(List<File> files, File file, String encoding, boolean isAppend) {
		OutputStream outputStream = null;
		InputStream inputStream = null;
		try {
			for (File file2 : files) {
				System.out.println("开始合并文件" + file2.getPath() + "...");
				if (!file2.canRead()) {
					throw new Exception("没有读取权限!\nat " + file2.getPath());
				}
				inputStream = new FileInputStream(file2);
				outputStream = new FileOutputStream(file, isAppend);
				byte[] buffer = new byte['Ѐ'];
				int n = 0;
				while ((n = inputStream.read(buffer)) != -1) {
					outputStream.write(buffer, 0, n);
				}
				outputStream.flush();
				outputStream.close();
				inputStream.close();
				System.out.println("合并文件" + file2.getPath() + "成功!");
				isAppend = true;
			}
			System.out.println("合并程序运行结束!\nat " + new Date());
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				outputStream.close();
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static boolean mergeFilesToOneFile(String directoryPath, String reg, String path, String fileName,
			String encoding, boolean isAppend) {
		String filePath = path + "/" + fileName;
		try {
			return mergeFilesToOneFile(directoryPath, reg, filePath, encoding, isAppend);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean mergeFilesToOneFile(String directoryPath, String reg, String filePath, String encoding,
			boolean isAppend) throws Exception {
		File directoryFile = new File(directoryPath);
		if (!directoryFile.isDirectory()) {
			throw new Exception("文件夹路径错误![文件不能转换为文件夹]");
		}
		File file = new File(filePath);
		if (file.isDirectory()) {
			throw new Exception("文件路径错误!文件夹不能转换为文件]");
		}
		List<File> files = filesFilter(getFilesFromDirectory(directoryFile), reg);
		return mergeFilesToOneFile(files, file, encoding, isAppend);
	}

	public static boolean mergeFilesToOneFile(String propertyFilePath, String propertyName, String filePath,
			String encoding, String reg, boolean isAppend, boolean isAllPropertyReadByPropertyFile) {
		try {
			System.out.println("正在读取" + propertyFilePath + "...");
			InputStream in = new BufferedInputStream(new FileInputStream(propertyFilePath));
			Properties p = new Properties();
			p.load(in);
			String directoryPath = p.getProperty(propertyName);
			if (isAllPropertyReadByPropertyFile) {
				reg = p.getProperty("reg");
				filePath = p.getProperty("filePath");
				encoding = p.getProperty("encoding");
				isAppend = Boolean.parseBoolean(p.getProperty("isAppend"));
			}
			System.out.println("读取完毕,正在合并...");
			return mergeFilesToOneFile(directoryPath, reg, filePath, encoding, isAppend);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static List<File> getFilesFromDirectory(File f) {
		List<File> files = new ArrayList<File>();
		System.out.println("开始扫描文件夹" + f.getPath() + "...");
		File[] ff = f.listFiles();
		File[] arrayOfFile1;
		int j = (arrayOfFile1 = ff).length;
		for (int i = 0; i < j; i++) {
			File child = arrayOfFile1[i];
			if (child.isDirectory()) {
				System.out.println("发现文件夹:" + child.getPath());
				files.addAll(getFilesFromDirectory(child));
			} else {
				files.add(child);
				System.out.println("发现文件:" + child.getPath());
			}
		}
		System.out.println("文件夹" + f.getPath() + "扫描结束!");
		return files;
	}

	public static List<String> fileNamesFilter(List<String> fileNames, String reg) {
		List<String> list = new ArrayList<String>();
		for (String str : fileNames) {
			if (str.matches(".*\\" + reg + "$")) {
				list.add(str);
			}
		}
		return list;
	}

	public static List<File> filesFilter(List<File> files, String reg) {
		List<File> list = new ArrayList<File>();
		for (File file : files) {
			if (file.getName().matches(".*\\" + reg + "$")) {
				list.add(file);
			}
		}
		return list;
	}
}

猜你喜欢

转载自xh32t03.iteye.com/blog/2317629