• 循环处理目录下文件框架


    经过会遇到对指定目录下文件循环处理的情况,往往是写个函数进行循环处理,每次都这样,代码重复量很大。

    于是想到总结一下经验,简化后续做目录下文件循环处理流程。

    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;
    	}
    }
    


    
  • 相关阅读:
    Jing : 记录屏幕上的图像、录像,拿来与朋友共享
    VistaDB 数据库,.NET的新选择
    获取指定网站的屏幕抓图
    XOOXML 操控 Excel 2007的组件
    MySQL 的一个奇怪错误
    又一个.NET代码生成器
    A .NET API for the Google Maps Geocoder
    ASP.NET + MySQL 开发笔记 MembershipProvider 和 RoleProvider 用法
    Educational Codeforces Round 96 (Rated for Div. 2)
    Codeforces Round #676 (Div. 2)
  • 原文地址:https://www.cnblogs.com/jerry1999/p/4175923.html
Copyright © 2020-2023  润新知