java 如何从java中的文本文件中跳过某些行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10008026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to skip certain line from Text file in java?
提问by Serag Alzetnani
I am currently learning Java and I have faced this problem where I want to load a file that consists a huge number of lines (I am reading the file line by line ) and the thing I want to do is skip certain lines (pseudo-code).
我目前正在学习 Java,我遇到了这个问题,我想加载一个包含大量行的文件(我正在逐行读取文件),我想做的是跳过某些行(伪代码) )。
the line thats starts with (specific word such as "ABC")
I have tried to use
我试过用
if(line.startwith("abc"))
But that didn't work. I am not sure if I am doing it wrong, that's why I am here asking for a help, below part of the load function:
但这没有用。我不确定我是否做错了,这就是为什么我在这里寻求帮助,在加载功能的下面部分:
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(......);
[...]
采纳答案by vikiiii
Try
尝试
if (line.toUpperCase().startsWith(-"ABC")){
//skip line
} else {
//do something
}
This will converts the line
to all the Upper Characters by using function toUpperCase()
and will check whether the string starts with ABC
.
这将line
使用函数将 转换为所有大写字符toUpperCase()
,并将检查字符串是否以ABC
.
And if it is true
then it will do nothing(skip the line) and go into the else
part.
如果是,true
那么它将什么都不做(跳过该行)并进入该else
部分。
You can also use startsWithIgnoreCase
which is a function provided by the Apache Commons. It takes the two string arguments.
您还可以使用startsWithIgnoreCase
which 是Apache Commons提供的功能。它需要两个字符串参数。
public static boolean startsWithIgnoreCase(String str,
String prefix)
This function return boolean. And checks whether a String starts with a specified prefix.
此函数返回布尔值。并检查字符串是否以指定的前缀开头。
It return true if the String starts with the prefix , case insensitive.
如果 String 以 prefix 开头,则返回 true,不区分大小写。
回答by Boosty
If the case isn't important try using the StringUtils.startsWithIgnoreCase(String str,
String prefix)
of Apache Commons
如果情况是不使用的重要尝试StringUtils.startsWithIgnoreCase(String str,
String prefix)
的Apache的百科全书
This function return boolean.
Usage:
用法:
if (StringUtils.startsWithIgnoreCase(-line, "abc")){
//skip line
} else {
//do something
}
回答by Christian Kuetbach
If you have large a input File, you code will create a OutOfMemoryError
. there is nothing you can do against it without editing te code (adding more memory will fail, if the file gets bigger).
如果您有一个大的输入文件,您的代码将创建一个OutOfMemoryError
. 如果不编辑代码(如果文件变大,添加更多内存将失败),您无能为力。
I beleave you store the selected lines in memory. If the file gets lager (2GB or so) you'll have 4GB in memory. (The old Value of the String
and the new one).
我相信你将选定的行存储在内存中。如果文件变得更大(2GB 左右),您将拥有 4GB 的内存。(旧值String
和新值)。
You have to work with streams to solve this.
你必须使用流来解决这个问题。
Create a FileOutpuStream
, and write the selcted line into that Stream.
创建一个FileOutpuStream
,并将所选行写入该流。
Your method must be changed. For a large input yo cannot return a String:
你的方法必须改变。对于大输入,您不能返回字符串:
public String loadfile(...){
You can return a Stream
or a file.
您可以返回一个Stream
或一个文件。
public MyDeletingLineBufferedReader loadFile(...)
回答by Chandra Sekhar
you can use:
您可以使用:
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
String lineString;
try{
while((lineString = br.readLine()) != null) {
if (lineString.toUpperCase().startsWith(-"abc")){
//skip
} else {
//do something
}
}
}
or
或者
static boolean startsWithIgnoreCase(String str, String prefix)
method in org.apache.commons.lang.StringUtilslike below.
static boolean startsWithIgnoreCase(String str, String prefix)
org.apache.commons.lang.StringUtils 中的方法如下。
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
String lineString;
try{
while((lineString = br.readLine()) != null) {
if (StringUtils.startsWithIgnoreCase(-lineString, "abc")){
//skip
} else {
//do something
}
}
}