java.file文件合并 博客分类: Java java文件合并
程序员文章站
2024-03-16 12:20:34
...
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; } }
上一篇: 六大排序算法的实现
推荐阅读
-
java.file文件合并 博客分类: Java java文件合并
-
将类文件打包生成jar文件 博客分类: java
-
将类文件打包生成jar文件 博客分类: java
-
swing(二)-----五个可执行jar文件 博客分类: java Swing
-
java中载入properties文件的方法 博客分类: core java javaproperties载入文件
-
java中载入properties文件的方法 博客分类: core java javaproperties载入文件
-
解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法 博客分类: java 语言maven
-
解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法 博客分类: java 语言maven
-
form表单的Action.java文件和jsp之间数据传递 博客分类: web框架 strutsjavajspJavaScriptXHTML
-
ssh三大框架代码的自动生成 博客分类: java 自动生成配置文件自动生成代码ssh自动生成三大框架自动生成