我基本上想忽略某些带有字符的行,比如有一行
// hello, i'm bill
我想在阅读时忽略该行,因为它包含字符“//”。我怎样才能做到这一点?我尝试了方法 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;
}
更新:这是我的主要方法:
try{
ReadFile files = new ReadFile(file.getPath());
String[] anyLines = files.OpenFile();
}