該文件以大約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);
}
});
}
你用當前的代碼看到了什麼問題? – vptheron
用'if(line == endOfSyllabus){'':不要在Java中用'=='來比較'String'值;改用'String#equals'。 – rgettman
當你到達文件結尾時''while'循環將無法正常工作。 – Keppil