统计 text/html 文档类型

来源:互联网 发布:stc12c5a16s2 编程 编辑:程序博客网 时间:2024/05/16 18:20

      使用heritrix抓取的网页存储在jobs/crawler/mirror下,判断抓取的文件是否是text/html 文档类型,只需要判断文件中是否含有 “text/html” 字符串,使用BufferedReader类中的readLine()方法读取文件每一行,检查其是否包含该串,如果有则说明是text/html 文档,否则,一直读到文件末尾仍未包含,则不是text/html 文档。

      遍历jobs文件夹下每个crawler文件夹,找到其目录下的mirror文件夹,对其进行深度优先搜索,找到目录树的每一片叶子(即文件),检索文件,检查是否包含“text/html”字符串,如果有,则计数器加1

publicvoid searchFile(File file) {

       File[] subDirectory = null;

       BufferedReader brFile = null;

       String textLine = null;

       if(!file.isDirectory()) {

           /*

            * 读取文件,判断是否含有 TEXT_HTML = "text/html"

            */

           try {

              brFile = new BufferedReader(new FileReader(file));

              while((textLine = brFile.readLine())!=null) {

                  if(textLine.contains(TEXT_HTML)) {

                     count ++;

                     break;

                  }

              }

           } catch(IOException e) {

              e.printStackTrace();

           } finally {

              try {

                  if(brFile !=null) {

                     brFile.close();

                     brFile = null;

                  }

              } catch(IOException e) {

                  e.printStackTrace();

              }

           }

       } else {

           subDirectory = file.listFiles();

           for(int i = 0; i < subDirectory.length; i++) {

              searchFile(subDirectory[i]);

           }

       }

}

原创粉丝点击