[英]Java Text Input: How to ignore lines starting with certain characters in them?
I basically want to ignore certain lines with characters in them, like if there's a line 我基本上想忽略某些带有字符的行,就像是否有一行
// hello, i'm bill
I want to ignore that line while reading it because it contains the character "//". 我想在读取时忽略该行,因为它包含字符“ //”。 How can I do that?
我怎样才能做到这一点? I tried method skip(), but it gives me errors.
我尝试了方法skip(),但它给了我错误。
public String[] OpenFile() throws IOException {
FileReader reader = new FileReader(path);
BufferedReader textReader = new BufferedReader(reader);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int i;
for (i=0; i<numberOfLines; i++) {
textData[i] = textReader.readLine();
}
// close the line-by-line reader and return the data
textReader.close();
return textData;
}
int readLines() throws IOException {
FileReader reader = new FileReader(path);
BufferedReader textReader = new BufferedReader(reader);
String line;
int numberOfLines = 0;
while ((line = textReader.readLine()) != null) {
// I tried this:
if (line.contains("//")) {
line.skip();
}
numberOfLines++;
}
reader.close();
return numberOfLines;
}
Update: HERE's MY MAIN METHOD: 更新:这是我的主要方法:
try{
ReadFile files = new ReadFile(file.getPath());
String[] anyLines = files.OpenFile();
}
while ((line = textReader.readLine()) != null) {
// I tried this:
if (line.contains("//")) {
continue;
}
numberOfLines++;
}
note that continue
might seem a bit goto like and be prone to critique 请注意,
continue
可能看起来有点像,并且容易受到批评
edit here's what you are after (note this doesn't need the countLines method) 编辑下面的内容(请注意,这不需要countLines方法)
public String[] OpenFile() throws IOException {
FileReader reader = new FileReader(path);
BufferedReader textReader = new BufferedReader(reader);
List<String> textData = new LinkedList<String>();//linked list to avoid realloc
String line;
while ((line = textReader.readLine()) != null) {
if (!line.contains("//")) textData.add(line);
}
// close the line-by-line reader and return the data
textReader.close();
return textData.toArray(new String[textData.size()]);
}
As Andrew Thompson points out, it would be best to read the file line by line into an ArrayList. 正如Andrew Thompson指出的那样,最好将文件逐行读取到ArrayList中。 Pseudo-Code:
伪代码:
For Each Line In File
If LineIsValid()
AddLineToArrayList()
Next
UPDATE to fix your actual code: 更新以修复您的实际代码:
public String[] OpenFile() throws IOException {
FileReader reader = new FileReader(path);
BufferedReader textReader = new BufferedReader(reader);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int BufferIndex = 0;
String line;
while ((line = textReader.readLine()) != null) {
if (line.trim().startsWith("//")) {
// Don't inject current line into buffer
}else{
textData[BufferIndex] = textReader.readLine();
BufferIndex = BufferIndex + 1;
}
}
// close the line-by-line reader and return the data
textReader.close();
return textData;
}
In your ReadLines() Function: 在您的ReadLines()函数中:
while ((line = textReader.readLine()) != null) {
if (line.trim().startsWith("//")) {
// do nothing
}else{
numberOfLines++;
}
}
Basically, you're on the right track. 基本上,您在正确的道路上。
Note : You may be interested in the startsWith() string function 注意 :您可能对startsWith()字符串函数感兴趣
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.