그래서 txtfile에서 코드 조각을 추출하려고합니다. 조각의 시작은 "#EMPIRES"로 표시되고 끝은 '#'으로 시작하는 다른 문자열로 표시됩니다. 내 프로그램 그러나 결코 조각의 시작을 발견하고 파일의 끝에 도달 할 때까지 계속.텍스트 파일이 완전하게 읽히지 않는 이유는 무엇입니까?
문제의 원인을 파악하기 위해 먼저 발견 한 모든 줄을 인쇄 해 보았습니다. 여기 다른 문제가 발생했습니다. 내 코드는 이미 "# EMPIRES"에 도달하기 훨씬 전에 새로운 행을 찾는 것을 중지합니다.
public String getEmpirestxt(String fileName) {
Scanner sc;
try {
sc = new Scanner(new File(fileName));
String currentLine = sc.nextLine();
StringBuilder empiresText = new StringBuilder(currentLine);
while (!currentLine.startsWith("# EMPIRES")) {
currentLine = sc.nextLine();
System.out.println(currentLine);
}
currentLine = sc.nextLine();
while (sc.hasNextLine() && currentLine.charAt(0)!='#') {
empiresText.append("\n").append(sc.nextLine());
}
return empiresText.toString();
} catch (FileNotFoundException ex) {
System.out.println("Landed_Titles.txt not found.");
}
return null;
}
TEXTFILE 자체 : https://www.wetransfer.com/downloads/a1093792d5ac54b6ccce04afecb9357f20140402095042/505fca
"currentLine = sc.nextLine();"을 주석 처리 할 수 있습니까? 그리고 그것이 효과가 있는지보십시오. – Boris
아무 것도 바뀌지 않기 때문에 첫 번째 루프에 갇혀있는 것보다 ... – BURNS
[Java 스캐너가 전체 파일을 통과하지 못함] (http://stackoverflow.com/questions/8330695/java-scanner-not-going- 전체 파일 통과) –