如何从java中的Text文件中跳过某些行?

Ser*_*ani 13 java io out-of-memory

我目前正在学习Java,我遇到了这个问题,我想加载一个包含大量行的文件(我逐行读取文件),我想要做的就是跳过某些行(伪代码) ).

the line thats starts with (specific word such as "ABC")
Run Code Online (Sandbox Code Playgroud)

我试过用

if(line.startwith("abc"))
Run Code Online (Sandbox Code Playgroud)

但那没用.我不确定我做错了,这就是为什么我在这里寻求帮助,在加载功能的一部分下面:

public String loadfile(.........){

//here goes the variables 

try {

        File data= new File(dataFile);
        if (data.exists()) {
            br = new BufferedReader(new FileReader(dataFile));
            while ((thisLine = br.readLine()) != null) {                        
                if (thisLine.length() > 0) {
                    tmpLine = thisLine.toString();
                    tmpLine2 = tmpLine.split(......);
                    [...]
Run Code Online (Sandbox Code Playgroud)

vik*_*iii 9

尝试

if (line.toUpperCase().startsWith(­"ABC")){
    //skip line
} else {
    //do something
}
Run Code Online (Sandbox Code Playgroud)

这将line使用函数toUpperCase() 将所有上部字符转换为所有上部字符,并将检查字符串是否以字符串开头ABC.

如果它是true那么它将什么都不做(跳过线)并进入该else部分.

您还可以使用Apache CommonsstartsWithIgnoreCase提供的功能.它需要两个字符串参数.

public static boolean startsWithIgnoreCase(String str,
                                           String prefix)
Run Code Online (Sandbox Code Playgroud)

这个函数返回布尔值.并检查String是否以指定的前缀开头.

如果String以前缀开头,则不返回true,不区分大小写.