2017-01-01 3 views
-6

모든 줄이 아닌 텍스트 파일에서 특정 줄을 읽고 싶습니다. 나는 모든 라인을 얻을 수 있어요이 코드를 사용하여텍스트 파일의 줄 읽기

public class BufferedReaderDemo { 

    public static void main(String[] args) throws IOException { 

     FileReader fr = new FileReader("Demo.txt"); 
     BufferedReader br = new BufferedReader(fr); 
     String line = br.readLine(); 

     while(line!=null) 
     { 
      System.out.println(line); 
      line = br.readLine(); 
     } 

     br.close(); 
    } 
} 

: 다음 코드를 시도했다. 하지만 "네임 스페이스"로 시작하고 "콘솔"로 끝나는 콘솔의 특정 2-3 행을 인쇄하려고합니다.

어떻게하면됩니까?

+1

* "어떻게 할 수 있습니까?"* if 문을 사용하여. – Andreas

+0

스택 오버플로에 오신 것을 환영합니다. http://stackoverflow.com/help/how-to-ask를 읽으십시오. 읽고있는 데이터를 보여 주면 도움이 될 수 있습니다. – Mikkel

답변

0

사용 String.startsWithString.endsWith는 :

당신이 라인이 일부 특정 단어가 포함되어 있는지 알고 싶은 경우에 당신은 선택의 여지가
while(line!=null) 
{ 
    if(line.startsWith("namespace") && line.endsWith("Console")) { 
     System.out.println(line); 
    } 
    line = br.readLine(); 
} 
1

, 당신은 그것을 읽을 수 있습니다.

이 줄만 인쇄하려는 경우 인쇄하기 전에 조건을 추가 할 수 있습니다.

String line = br.readLine(); 

while(line!=null){ 
    if (line.startsWith("namespace") && line.endsWith("Console")){ 
     System.out.println(line); 
    } 
    line = br.readLine(); 
}