简体   繁体   English

Java缓冲读取器将一部分URL读取到String中然后挂起(是否有大小限制?)

[英]Java Buffered Reader Reads In Part of the URL to String then Hangs (Is there a size limit?)

I am trying to read a URL into a String and it starts reading in the URL and then just stops at the same line every time and hangs. 我正在尝试将URL读入字符串,它开始在URL中读取,然后每次都停在同一行并挂起。 This happens with any URL and it happens when I run the program from the command line on Windows 7. When I run the same program in Eclipse it never hangs and reads in the entire website. 任何URL都会发生这种情况,当我在Windows 7上从命令行运行该程序时就会发生这种情况。当我在Eclipse中运行相同的程序时,它永远不会挂起并读取整个网站。

It always gets to "got to line 2" and inside of the while loop but never to "line 3". 它总是在“进入第2行”和while循环内部,而从不进入“第3行”。

Here is the code I am using below. 这是我在下面使用的代码。 Is there some type of a size limit or something when doing it right through Windows on the command line? 通过命令行在Windows上执行大小限制时是否有某种类型的限制?

URL link = new URL("http://www.yahoo.com");
System.out.println("got to this line 1");
BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));

System.out.println("got to this line 2");
        //InputStream in = link.openStream();
        String inputLine = "";
        int count = 0;
        while ((inputLine = in.readLine()) != null)
        {
            site = site + "\n" + inputLine;
            System.out.println(inputLine);
        }
        System.out.println("exited the while loop.");
        in.close();
        System.out.println("got to this line 3");

I took that code, put it in a main method, added a site variable as String site = ""; 我获取了该代码,并将其放在main方法中,并添加了一个site变量,如String site = ""; , compiled it with IntelliJ, and ran it from a Windows 7 command prompt, and it worked fine: ,并使用IntelliJ对其进行编译,然后从Windows 7命令提示符下运行它,效果很好:

> java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02, mixed mode)

Output of the program: 程序输出:

got to this line 1
got to this line 2
<all of the page source>
exited the while loop.
got to this line 3

Does it work the same if you comment out the line that prints out each line as its read in? 如果您注释掉在读入时打印出每一行的行,它的工作原理是否相同?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM