Java Grep Library [英] Java Grep Library

查看:79
本文介绍了Java Grep Library的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么好的,简单的Java Grep Library?我并不反对本地代码或脚本,我会这样做,但出于我的目的,吞吐量并不是什么大问题,并且将它全部放在一个整洁的包中会很好。



更新:对不起。我知道java.regex,现在我只是相当忙碌和累了。我正在寻找的是有效地将java正则表达式与经历一组文件并重写它们的东西。我承认,这不会太难写。我只是好奇,如果这已经存在。

解决方案

我不知道一个复杂的grep库强壮的文字,但你是对的:写作不难。
我建议结合使用 commons-io String.matches(someRegex)


$ b $

  public class Grep extends DirectoryWalker 
{
public Grep(){
super();

$ b $ public清单(File startDirectory){
List results = new ArrayList();
walk(startDirectory,results);
返回结果;

$ b保护布尔值handleDirectory(文件目录,
int depth,集合结果){
//决定是否为递归搜索处理(子)目录
返回true;


protected void handleFile(File file,int depth,Collection results)
{
LineIterator it = FileUtils.lineIterator(file,UTF-8) ;
尝试{
while(it.hasNext()){
String line = it.nextLine();
if(line.matches(myRegEx)){
results.add(file);


$ b finally {LineIterator.closeQuietly(it);}
}
}






更新 Marco指出 Unix4j 这是一个非常有趣的库,它模拟了使用Java方法链接的unix流水线 | 。支持 grep 以及 cat,cd,cut,echo,find,grep,head,ls,sed,sort,tail,uniq,wc ,驳船


Is there any good, simple Java Grep Library? I'm not opposed to native code, or scripting, and I'll do it, but for my purposes, throughput is not a huge deal, and it would be nice to have it all in one tidy package.

UPDATE: Sorry. I know about java.regex, I just happen to be fairly busy and tired right now. What I'm looking for is something that efficiently combines java regex with going through a set of files and rewriting them. This wouldn't be too hard to write, I admit. I was just curious if that exists already.

解决方案

I'm not aware of a sophisticated grep librarystrong text, but you are right: it's not hard to write. I suggest a combination of commons-io and String.matches(someRegex):

public class Grep extends DirectoryWalker
{
    public Grep(){
        super();
    }

    public List clean(File startDirectory){
      List results = new ArrayList();
      walk(startDirectory, results);
      return results;
    }

    protected boolean handleDirectory(File directory,
                                      int depth, Collection results){
      // Decide if a (sub) directory will be handled for recursive search
      return true;
    }

    protected void handleFile(File file, int depth, Collection results)
    {
        LineIterator it = FileUtils.lineIterator(file, "UTF-8");
        try{
            while (it.hasNext()){
                String line = it.nextLine();
                if(line.matches("myRegEx")){
                    results.add(file);
                }
            }
         }
         finally {LineIterator.closeQuietly(it);}
    }
}


Update Marco pointed out Unix4j which is a quite interesting library which emulates the unix pipelining | with Java method chaining. grep is supported as well as cat, cd, cut, echo, find, grep, head, ls, sed, sort, tail, uniq, wc, barges.

这篇关于Java Grep Library的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆