java.net 源代码阅读器跳行

标签 java

此代码用于下载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()) {
      }
    }
  }         
}   

最佳答案

每次迭代读取两行:

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

      linesOfSource.add(s.readLine());

  }

应该是:

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

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

String sourceCodeLine = s.readLine();

关于java.net 源代码阅读器跳行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9096861/

相关文章:

java - 无法完成 MapActivity

java - 带有自定义注释的 JSON 到 POJO

java - Android GC 考虑因素——GC 何时运行,是否可以从代码中跟踪其运行状态?

java - Websphere Application Server 7 的日志记录框架

java - Azkaban 流参数

java - java中的二维数组

java - 从 SOQL 查询获取列的元数据

java - java 1.6 注解处理时获取实际类型而不是类型参数

java - 使用最新的 JRE 而不是旧的 Java 版本,我能保证它会工作吗?

java - Hadoop文件系统的绝对路径是什么