zoukankan      html  css  js  c++  java
  • 从指定的路径中查找含有特殊字符串的文件

    import java.io.*;
    import java.util.*;
    import java.util.concurrent.*;
    public class SearchFile
    {
        public static void main(String ... strings)
        {
            final int FILE_Queue_SIZE = 10;
            final int SEARCH_THREADS = 100;

            Scanner in = new Scanner(System.in);
            System.out.println("enter your base directory:");
            String directory = in.nextLine();
            System.out.println("enter your keyword");
            String keyword = in.nextLine();

            ExecutorService pool= Executors.newCachedThreadPool();
            MatchCounter counter = new MatchCounter(new File(directory),keyword,pool);
            Future<Integer> result = pool.submit(counter);
            try
            {
                System.out.println("result.get()=" + result.get() + "matching files.");
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            pool.shutdown();
            int largestPoolSize = ((ThreadPoolExecutor)pool).getLargestPoolSize();
            System.out.println("largestPoolSize=" + largestPoolSize);
        }
    }
    class MatchCounter implements Callable<Integer>
    {
        private File directory;
        private    String keyword;
        private ExecutorService pool;
        private int count;
        public MatchCounter(File directory, String keyword, ExecutorService pool)
        {
            this.directory = directory;
            this.keyword = keyword;
            this.pool = pool;
        }
        public Integer call()
        {
            count = 0;
            try
            {
                File[] files = directory.listFiles();
                ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();
                for(File file:files)
                {
                    if(file.isDirectory())
                    {
                        MatchCounter counter = new MatchCounter(file,keyword,pool);;
                        Future<Integer> result = pool.submit(counter);
                        results.add(result);
                    }else
                    {
                        if (search(file))
                        {
                            count++;
                            System.out.println("路径:" + file.getAbsolutePath());
                        }
                    }
                }
                for(Future<Integer> result:results)
                {
                    try
                    {
                        count += result.get();
                        System.out.println("count=" + count + "-----" + "result.get()=" + result.get());
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                
            }catch (Exception e)
            {
                e.printStackTrace();
            }
            return count;
        }
        public boolean search(File file)
        {
            try
            {
                Scanner in = new Scanner(new FileInputStream(file));
                boolean found = false;
                while(!found && in.hasNextLine())
                {
                    String line = in.nextLine();
                    if(line.contains(keyword)) found = true;
                }
                in.close();
                return found;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }

    我很小我很弱,我在慢慢成长!
  • 相关阅读:
    ubuntu配置apache支持sqlite数据库
    linux中的(),(()),[],[[]],{}的作用
    javascript问题积累
    羞愧的开始,成熟的开始,努力的开始
    asp发送邮件代码
    css&html的问题积累
    应用phpcms时遇到的问题及smarty标签的应用
    js正则积累
    产生一个int数组,长度为100,并向其中随机插入1100,并且不能重复。按照数组下标输出结果。
    不同项目之间的基础dll共用问题
  • 原文地址:https://www.cnblogs.com/lvzhanhui/p/xiaoqiaolv_search_char.html
Copyright © 2011-2022 走看看