File 搜索列表并返回整个条目

File 搜索列表并返回整个条目,file,list,search,groovy,split,File,List,Search,Groovy,Split,我尝试读取一个文本文件并找到一个给定的参数,如果这是真的,它应该会向我提供整个列表条目 这是文件中的输入:100 0100045391 0400053454 0502028765251 ABH ZL1 1560112 07.06.2010 100 0100045394 0400055024 050208766382 ABH ZL1 1601944 21.06.2010 但是现在我只能检查这个参数是否在列表中,或者给定的参数本身 import groovy.util.CharsetToolkit;

我尝试读取一个文本文件并找到一个给定的参数,如果这是真的,它应该会向我提供整个列表条目

这是文件中的输入:100 0100045391 0400053454 0502028765251 ABH ZL1 1560112 07.06.2010 100 0100045394 0400055024 050208766382 ABH ZL1 1601944 21.06.2010

但是现在我只能检查这个参数是否在列表中,或者给定的参数本身

import groovy.util.CharsetToolkit;
//Pathname
def pathname = "C:/mySupport-eclipse/trackandtrace.txt"
//Error State
int errorCode = 0

def bsknr = "0100045553"
//Define new file
def file = new File(pathname)

if(!file.exists())
    {
        errorCode = 1   
    }
    else
    {
        //Read lines, seperated by tab
        file.splitEachLine ('\t') { 
            list -> list

            println list.findAll {it.contains(bsknr)}

    }
}

假设您的意思是您的输入文件是:

100 0100045391  0400053454  0502028765251   ABH ZL1 156011207.06.2010
100 0100045394  0400055024  0502028766382   ABH ZL1 160194421.06.2010
假设您的意思是“如何获得包含此字符串的行列表”(我不知道“但目前我可以检查此参数是否在列表中或给定参数本身。”意思),那么您可以执行以下操作:

//Pathname
def pathname = "C:/mySupport-eclipse/trackandtrace.txt"
//Error State
int errorCode = 0

def bsknr = "0100045553"
def lines = []

//Define new file
def file = new File( pathname )

if(!file.exists()) {
  errorCode = 1   
}
else {
  //Read lines, seperated by tab
  file.eachLine { line ->
    if( line.split( /\t/ ).grep { bsknr } ) lines << line 
  }
}

println lines
//路径名
def pathname=“C:/mySupport eclipse/trackandtrace.txt”
//错误状态
int errorCode=0
def bsknr=“0100045553”
def行=[]
//定义新文件
def file=新文件(路径名)
如果(!file.exists()){
错误代码=1
}
否则{
//读行,用制表符分隔
file.eachLine{line->

如果(line.split(/\t/).grep{bsknr})行您可以使用正则表达式,它将返回包含参数的整行。与Groovy的内置方法一起,您可以得到如下结果:

def lines = file.filterLine { line -> line ==~ /.*\t${bsknr}\t.*/ }
如果希望
成为字符串,可以执行以下操作:

def linesAsString = lines.toString()
如果希望它们成为一个列表,可以执行以下操作:

def linesAsList = lines.toString().tokenize("\n")