After processing the files in a specified directory cyclically, a function is often written for loop processing. Every time this happens, the Code repeats a lot.
So I thought of summing up my experience and simplifying the subsequent file loop processing process in the directory.
1. The simplest framework can be used to process directories cyclically. For how each file needs to be processed, it is implemented by sub-classes.
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. Reading a file in the unit of behavior. This method is applicable to traversing all files in a directory and reading and processing all lines in the file.
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. Read files in behavior units. This method is suitable for traversing all files in a directory and updating all lines in the file.
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;}}
Zookeeper