循环处理目录下文件框架
2014-07-05 21:36 每天努力一点点 阅读(194) 评论(0) 编辑 收藏 举报经过会遇到对指定目录下文件循环处理的情况,往往是写个函数进行循环处理,每次都这样,代码重复量很大。
于是想到总结一下经验,简化后续做目录下文件循环处理流程。
1.最简框架,可以对目录进行循环处理,对于每个文件需要如何处理则由子类实现
package cn.jerryhouse.util.file; import java.io.File; public abstract class FileProcessor { private long totalFileCount = 0; private long processedFileCount = 0; public void processFiles(File[] dirs) throws Exception { for (File file : dirs) { processFile(file); } } public void processFile(File file) throws Exception { if (file.isFile()) { if (isFileAccepted(file)) { handleFile(file); processedFileCount++; } totalFileCount++; } else { File[] files = file.listFiles(); for (File fileInDir : files) { processFile(fileInDir); } } } protected boolean isFileAccepted(File file) throws Exception { return true; } protected abstract void handleFile(File file); public long getTotalFileCount() { return totalFileCount; } public long getProcessedFileCount() { return processedFileCount; } }
2. 以行为单位进行文件读取,适用于要遍历目录下所有文件,对文件中所有行进行读取处理
package cn.jerryhouse.util.file; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public abstract class LineReaderProcessor extends FileProcessor { private long totalFileCount = 0; private long processedFileCount = 0; public void processFiles(File[] dirs) throws Exception { for (File file : dirs) { processFile(file); } } public void processFile(File file) throws Exception { if (file.isFile()) { if (isFileAccepted(file)) { handleFile(file); } } else { File[] files = file.listFiles(); for (File fileInDir : files) { processFile(fileInDir); } } } protected boolean isFileAccepted(File file) throws Exception { return true; } protected void handleFile(File file) { try { BufferedReader br = new BufferedReader(new FileReader(file)); String line; while((line=br.readLine())!=null) { readLine(file,line); } br.close(); } catch (Exception e) { e.printStackTrace(); } } protected abstract void readLine(File file,String line); public long getTotalFileCount() { return totalFileCount; } public long getProcessedFileCount() { return processedFileCount; } }
3. 以行为单位进行文件读取,适用于要遍历目录下所有文件,对文件中所有行进行更新处理
package cn.jerryhouse.util.file; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.util.UUID; public abstract class LineUpdateProcessor extends FileProcessor { private long totalFileCount = 0; private long processedFileCount = 0; private String NEW_LINE = System.getProperty("line.separator"); public void processFiles(File[] dirs) throws Exception { for (File file : dirs) { processFile(file); } } public void processFile(File file) throws Exception { if (file.isFile()) { if (isFileAccepted(file)) { handleFile(file); } } else { File[] files = file.listFiles(); for (File fileInDir : files) { processFile(fileInDir); } } } protected boolean isFileAccepted(File file) throws Exception { return true; } protected void handleFile(File file) { try { BufferedReader br = new BufferedReader(new FileReader(file)); File tmpFile = new File(tmpFilePath(file)); BufferedWriter bw = new BufferedWriter(new FileWriter(tmpFile)); String line; while((line=br.readLine())!=null) { String updatedLine = updateLine(file,line); bw.write(updatedLine+NEW_LINE); } br.close(); bw.close(); file.delete(); tmpFile.renameTo(file); } catch (Exception e) { e.printStackTrace(); } } private String tmpFilePath(File file) { String dir = file.getParent(); String filePath = dir+""+getUniqFileName(); return filePath; } private String getUniqFileName() { return UUID.randomUUID().toString(); } protected abstract String updateLine(File file,String line); public long getTotalFileCount() { return totalFileCount; } public long getProcessedFileCount() { return processedFileCount; } }