2012-02-01 4 views
0

이 코드는 html 파일 의 소스 코드를 다운로드하는 데 사용되지만 일부 줄은 건너 뜁니다. 그 이유는 무엇입니까? 그것은 다시 할당되기 전에java.net sourceCode reader skip lines

String sourceCodeLine = s.readLine(); 

그런 sourceCodeLine으로 아무것도 할 수 없다 : 당신이 밖으로 시작하기 때문에

import java.io.IOException; 
import java.net.*; 
import java.util.*; 
import java.io.*; 

public class downloadSource { 
    private URL url; 
    private URLConnection con; 

    // Construct the object with a new URL Object resource 
    downloadSource(String url) { 
    try { 
     this.url = new URL(url); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 
    } 

    // Returns an object instance 
    public BufferedReader SourceCodeStream() throws IOException { 
    return new BufferedReader(new InputStreamReader(this.url.openStream())); 
    } 


    public void returnSource() throws IOException, InterruptedException { 
    // FIX ENTRIE SOURCE CODE IS NOT BEING DWLOADED 

    // Instinate a new object by assigning it the returned object from 
    // the invoked SourceCodeStream method. 

    BufferedReader s = this.SourceCodeStream();  
    if(s.ready()) { 
     String sourceCodeLine = s.readLine(); 
     Vector<String> linesOfSource = new Vector(); 
     while(sourceCodeLine != null) { 
     sourceCodeLine = s.readLine(); 
     linesOfSource.add(s.readLine());    
     } 

     Iterator lin = linesOfSource.iterator(); 
     while(lin.hasNext()) { 
     } 
    } 
    }   
} 

답변

4

이 반복 당 두 줄을 읽고 줄도 으로 건너 뜁니다.

(210)
String sourceCodeLine = s.readLine(); 
2

그것은 다른 모든 라인은 첫 번째 줄을 누락하고있다. 루프 내에서 비슷한 문제가 발생했습니다.

대신에, 당신이 뭔가를 할 수 있습니다 :

while(sourceCodeLine != null) { 
     sourceCodeLine = s.readLine(); 

     linesOfSource.add(s.readLine()); 

    } 

가되어야한다 :

while(sourceCodeLine != null) { 
     linesOfSource.add(sourceCodeLine); 
     sourceCodeLine = s.readLine(); 
    } 

이 두 번째 루프는 첫 번째 추가

String sourceCodeLine; 
Vector<String> linesOfSource = new Vector(); 

while((sourceCodeLine = s.readLine()) != null) { 
    linesOfSource.add(sourceCodeLine); 
} 
0

귀하의 문제는

while(sourceCodeLine != null) { 
     sourceCodeLine = s.readLine(); 

     linesOfSource.add(s.readLine()); 

    } 

당신은 두 번 라인을 읽고 linesofSource에 하나를 추가 여기에있다.

이렇게하면 문제가 해결됩니다.

while(sourceCodeLine != null) {  
     linesOfSource.add(sourceCodeLine); // We add line to list 
     sourceCodeLine = s.readLine(); // We read another line 
    } 
2

당신은 당신이 readLine()가 실행될 때마다 반환 정보를 저장해야합니다 그래서, 새로운 라인을 읽고,하지만 당신은 그 일을하지 않는 readLine()를 호출 할 때마다. 다음과 같이 시도해보세요.

while((tmp = s.readLine()) != null){ 
    linesOfSource.add(tmp); 
}