File framework under the loop processing Directory

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.