• FileIterator


    我们在开发中,经常需要遍历一个目录下的所有文件,常用的办法就是使用一个函数递归遍历是常用的办法。例如:
    public static void iterateFile(File file) {
        
    if (file.isDirectory()) {
            
    if (file.getName().startsWith(".")) return;

            
    for (File item : file.listFiles()) {
                iterateFile(item);
            }
            
    return;
        }

        
    // do something 
    }

    但是递归函数的缺点就是扩展不方便,当然你对这个函数加入一个参数FileHandler,这样扩展性稍好一些,但是仍然不够好,比如说,不能根据遍历的需要中途停止遍历,加入Filter等等。我实现了一个FileIterator,使得遍历一个目录下的文件如何遍历一个集合中的元素一般操作。

    废话少说,代码如下:

    package net.wenshao;

    import java.io.File;
    import java.util.Iterator;
    import java.util.NoSuchElementException;

    public class FileIterator implements Iterator<File> {
        
    private static class State {
            
    final State parent;
            
    final File[] files;

            
    int index = 0;

            
    public State(State parent, File dir) {
                
    this.parent = parent;
                files 
    = dir.listFiles();
            }
        }

        
    private File current;

        
    private State state;

        
    public FileIterator(File file) {
            
    if (file.isDirectory()) {
                state 
    = new State(null, file);
                nextInternal();
            } 
    else {
                
    this.current = file;
                state 
    = null;
            }
        }

        @Override
        
    public boolean hasNext() {
            
    return current != null;
        }

        @Override
        
    public File next() {
            File rtValue 
    = current;

            
    if (rtValue == nullthrow new NoSuchElementException();

            nextInternal();

            
    return rtValue;
        }

        
    private void nextInternal() {
            current 
    = null;

            
    if (this.state == nullreturn;

            
    for (;;) {
                
    if (state.index >= state.files.length) {
                    state 
    = state.parent;
                    
    if (state == nullreturn;

                    state.index
    ++;
                    
    continue;
                }

                File file 
    = state.files[state.index];
                
                
    // 可以在此处加入Filters处理代码
                
                
    if (file.isDirectory()) {
                    state 
    = new State(state, file);
                    
    continue;
                }

                current 
    = file;
                state.index
    ++;
                
    break;
            }
        }

        @Override
        
    public void remove() {
            
    throw new UnsupportedOperationException();
        }
    }

    使用FileIterator的例子:
    File dir = new File("/home/wenshao/workspace");

    Iterator
    <File> iter = new FileIterator(dir);
    while (iter.hasNext()) {
        File file 
    = iter.next();
        System.out.println(file.getPath());
    }



  • 相关阅读:
    Windows下给你mac下的开发体验 windows+wsl+zsh+vscode
    Puppeteer在docker下踩坑记录
    egret EUI加载 JSZip打包DragonBones二进制文件
    nodeJS rsasha加密
    Elasticsearch 聚合字段后 获取聚合字段的单条详细信息
    【转发自用】Apache2.4服务器权限设置问题
    C#重写VIVO支付SDK VivoSignUtils
    VS2017 空白 node.js web应用程序
    less开发
    获取样式getComputedStyle
  • 原文地址:https://www.cnblogs.com/jobs/p/1214029.html
Copyright © 2020-2023  润新知