文件遍历器类

转载 2007年09月25日 10:22:00
/// <summary>
    /// 文件遍历器,不遍历子目录,这个用于遍历单目录
    /// </summary>
    class FilesFilder
    {
        /// <summary>
        /// 子成员,用于存储目录下的文件信息
        /// </summary>
        private Hashtable childElms;

        /// <summary>
        /// 遍历的根目录
        /// </summary>
        private String root;

        public String getPosition()
        {
            return root;
        }

       public String getFolderName()
        {
            String[] dirs = root.Split('//');
            return dirs[dirs.Length - 1];
        }

        /// <summary>
        /// 扩展名过滤器,存储需要的文件扩展名,若指定为空则不进行文件扩展名过滤
        /// </summary>
        private ArrayList filters;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="filters"></param>
        public FilesFilder(String folder, ArrayList filters)
        {
            childElms = new Hashtable();
            this.filters = filters;
            root = folder;
            DirectoryInfo dir = new DirectoryInfo(folder);
            tranversal(dir);
        }

        /// <summary>
        /// 遍历根目录下的文件
        /// </summary>
        /// <param name="dir"></param>
        private void tranversal(DirectoryInfo dir)
        {
            FileSystemInfo[] fsis = dir.GetFileSystemInfos();

            FileSystemInfo fsi;

            for (int i = 0; i < fsis.Length; i++)
            {
                fsi = fsis[i];

                if (fsi.GetType() == typeof(FileInfo) && isPassedFilters(fsi.Extension.ToString().ToUpper()))
                {
                    //childElms.Add(DirFileUtil.getFileNameWithoutPath(fsi.FullName));
                    childElms.Add(DirFileUtil.getFileNameWithoutPath(fsi.FullName),GetFilePrimaryInfo((FileInfo)fsi));
                }
            }
        }

       /// <summary>
        /// 判断是否通过了扩展名过滤器
        /// </summary>
        /// <param name="ext"></param>
        /// <returns></returns>
        private bool isPassedFilters(String ext)
        {
            if (filters == null) return true;
            if (filters.Count == 0) return true;
            ext = ext.Replace(".", "");

            return filters.Contains(ext);
        }

        /// <summary>
        /// 返回文件列表
        /// </summary>
        /// <returns></returns>
        public Hashtable getFiles()
        {
            return childElms;
        }

        /// <summary>
        /// 返回通过过滤的文件数量
        /// </summary>
        /// <returns></returns>
        public int getFileCount()
        {
            return childElms.Count;
        }

        private FilePrimaryInfo GetFilePrimaryInfo(FileInfo fsi)
        {
            FilePrimaryInfo info = new FilePrimaryInfo();

            info.Name = DirFileUtil.getFileNameWithoutPath(fsi.FullName);
            info.Path = fsi.FullName.Replace(info.Name,"");
            info.ModifyTime = fsi.LastWriteTime.ToString();
            info.Size = fsi.Length.ToString();

            return info;
        }
    }

    /// <summary>
    /// 文件夹遍历器,遍历子目录
    /// </summary>
    class FoldersFinder
    {
        /// <summary>
        /// 子成员
        /// </summary>
        private ArrayList childElms;

        /// <summary>
        /// 遍历的根目录
        /// </summary>
        private String root;

        /// <summary>
        /// 扩展名过滤器,存储需要的文件扩展名,若指定为空则不进行文件扩展名过滤
        /// </summary>
        private ArrayList filters;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="filters"></param>
        public FoldersFinder(String folder, ArrayList filters)
        {
            childElms = new ArrayList();
            root = folder;
            this.filters = filters;
            DirectoryInfo dir = new DirectoryInfo(folder);

            // 加入本身
            FilesFilder filesFilder = new FilesFilder(folder, filters);

            if (filesFilder.getFileCount() > 0)
            {
                childElms.Add(filesFilder);
            }

            tranversal(dir);
        }

        /// <summary>
        /// 遍历根目录下的子目录,将过滤过的文件夹加入
        /// </summary>
        /// <param name="dir"></param>
        private void tranversal(DirectoryInfo dir)
        {
            FileSystemInfo[] fsis = dir.GetFileSystemInfos();

            FileSystemInfo fsi;

            for (int i = 0; i < fsis.Length; i++)
            {
                fsi = fsis[i];

                if (fsi.GetType() == typeof(DirectoryInfo))
                {
                    FilesFilder filesFilder = new FilesFilder(fsi.FullName, filters);

                    if (filesFilder.getFileCount()>0)
                    {
                        childElms.Add(filesFilder);
                    }

                    tranversal((DirectoryInfo)fsi);
                }
            }
        }

        /// <summary>
        /// 返回目录列表
        /// </summary>
        /// <returns></returns>
        public ArrayList getFolders()
        {
            return childElms;
        }
    } 

2.一个辅助类,用于存储文件信息

    class FilePrimaryInfo : IComparable
    {
        private String name;
        private String path;
        private String size;
        private String modifyTime;

        private static String sortBy = "name";
        public static String SortBy
        {
            get { return sortBy; }
            set { sortBy = value; }
        }

        public int CompareTo(object obj)
        {
            if (!(obj is FilePrimaryInfo))
                throw new InvalidCastException("Not a valid Developer object.");

            FilePrimaryInfo filePrimaryInfo = (FilePrimaryInfo)obj;

            if (sortBy.Equals("NAME"))
            {
                return this.name.CompareTo(filePrimaryInfo.name);
            }
            else if (sortBy.Equals("SIZE"))
            {
                return this.size.CompareTo(filePrimaryInfo.size);
            }
            else if (sortBy.Equals("TIME"))
            {
                return this.modifyTime.CompareTo(filePrimaryInfo.modifyTime);
            }
            else
            {
                return this.name.CompareTo(filePrimaryInfo.name);
            }           
        }

        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        public String Path
        {
            get { return path; }
            set { path = value; }
        }

        public String Size
        {
            get { return size; }
            set { size = value; }
        }

        public String ModifyTime
        {
            get { return modifyTime; }
            set { modifyTime = value; }
        }
    }

 

3.使用方法:

// 创建扩展名过滤器,过滤器的扩展名大小写要注意,你也可以修改

ArrayList filters=new ArrayList();

filters.Add("bmp");

filters.Add("jpg");

// 遍历单目录

FilesFilder finder = new FilesFilder(@"c:/abc", filters);

// 遍历目录及其子目录

FoldersFinder finder = new FoldersFinder(@"c:/abc", filters);

 

相关文章推荐

编写一个遍历器类,类似于Iterator,并对他测试,看是都正确

今天看think in java 看到内部类时,有一程序例子是遍历类,但是书上设计的是在这个遍历类中有boolean end()、int next()方法, 所以自己也想实现一个遍历类, 并参考Ite...

文件遍历器

  • 2014-09-15 09:27
  • 132KB
  • 下载

Qt学习之路(36): Qt容器类之遍历器和隐式数据共享

前面说过,Qt容器类提供了两种遍历器:Java风格的和STL风格的。前者比较容易使用,后者则可以用在一些通过算法中,功能比较强大。   对于每一个容器类,都有与之相对应的遍历器:只读遍历器...

Qt学习之路(36): Qt容器类之遍历器和隐式数据共享

本文出自 “豆子空间” 博客,请务必保留此出处http://devbean.blog.51cto.com/448512/247353 前面说过,Qt容器类提供了两种遍历器:Java风格的和STL...

Qt学习之路(36): Qt容器类之遍历器和隐式数据共享

Qt学习之路(36): Qt容器类之遍历器和隐式数据共享 2009-12-21 23:58:33 标签:C++ 教程 学习 QT教程 Qt 原创作品,允许转载,转载时请务必以超链...

基址遍历器

  • 2017-05-13 21:14
  • 770KB
  • 下载

游戏遍历器开源

  • 2011-11-23 14:19
  • 780KB
  • 下载

脚本入门--数组和遍历器

unity学习、unity培训这里有很多U3D资源、U3D培训视频、 U3D教程、U3D常见问题、U3D项目源码, 脚本入门--数组和遍历器 一、数据类型:1、基本类型:①整形:sbyte、us...

Java----For增强与Iterator遍历器提取数据(附Map.Entry)

增强for循环: 1.操作数组 2.操作List集合 3.操作Map集合    1.map.values()法    2.map.keySet()法  【传统方法】    3.Map.En...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)