【问题标题】:java.net sourceCode reader skipping linesjava.net sourceCode阅读器跳行
【发布时间】:2012-02-01 13:50:35
【问题描述】:

此代码用于下载一个html文件的源代码 但它跳过了一些行,为什么会发生这种情况?

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()) {
      }
    }
  }         
}   

【问题讨论】:

    标签: java


    【解决方案1】:

    每次迭代读取两行:

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

    应该是:

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

    第二个循环将第一行添加到同样被跳过的linesOfSource:

    String sourceCodeLine = s.readLine();
    

    【讨论】:

      【解决方案2】:

      它缺少第一行和其他所有行,因为你从这个开始:

      String sourceCodeLine = s.readLine();
      

      那么在再次分配sourceCodeLine 之前,不要对其进行任何操作。您的循环中还有另一个类似的问题。

      相反,您可以这样做:

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

      【讨论】:

        【解决方案3】:

        每次调用readLine() 时它都会读取一个新行,因此您需要保存每次执行readLine() 时返回的信息,但您没有这样做。试试这样的:

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

        【讨论】:

          【解决方案4】:

          你的问题来了

           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
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-07-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-08-27
            • 1970-01-01
            相关资源
            最近更新 更多