该文件以我不需要的约200行背景信息开头。我试图跳过/忽略那200行,直到找到一个字符串。找到此字符串后,我希望能够继续处理文本文件的其余部分。
示例文本文件: (直到第240行是我需要跳过/忽略的所有行) http://pastebin.com/5Ay4ad6y
public static void main(String args[]) {
String endOfSyllabus = "~ End of Syllabus";
Path objPath = Paths.get("2014HamTechnician.txt");
if (Files.exists(objPath)) {
File objFile = objPath.toFile();
try (BufferedReader in = new BufferedReader(new FileReader(objFile))) {
String line = in.readLine();
while (line != null) {
line = in.readLine();
}
if(endOfSyllabus.equals(line) ){
restOfTextFile = line.split(endOfSyllabus);
}
}
System.out.println(restOfTextFile[0]);
}
catch(IOException e){
System.out.println(e);
}
}
else{
System.out.println(
objPath.toAbsolutePath() + " doesn't exist");
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new A19015_Form().setVisible(true);
}
});
}
答案 0 :(得分:0)
如果您知道正在查找的字符串
,则可以尝试此操作if (lineString.startsWith("insert exact string")) {
// ...
}
答案 1 :(得分:0)
怎么样:
boolean found = false;
for (String line; (line = in.readLine()) != null;) {
found = found || line.equals(endOfSyllabus);
if (found) {
// process line
}
}
答案 2 :(得分:0)
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
public class Test {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
List<String> lines = FileUtils.readLines(new File("test.txt"));
List<String> avLines = new ArrayList<>();
boolean valid = false;
for (String line : lines) {
if (line.trim().equals("~ End of Syllabus")) {
valid = true;
continue;
}
if (valid) {
avLines.add("\n"+line);
}
}
System.out.println(avLines.size());
}
}