BufferedReader does not indicate ready when it should

I am trying to read text from a web document using BufferedReader by InputStreamReader at the URL (to a file on some Apache server).

String result = "";
URL url = new URL("http://someserver.domain/somefile");
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(url.openStream(), "iso-8859-1"));

result += in.readLine();

Now it works fine. But, obviously, I would like for the reader not only to read one line, but as much as in the file.
If you look at the BufferedReader API, the following code should do just that:

while (in.ready()) {
    result += in.readLine();
}

those. read all lines while there are more lines, stop when there are no more lines. This code does not work, but the reader is simply never ready () = true!

I can even print the value of ready () right before reading the line (which reads the correct line from the file), but the reader will report “false”.

- ? BufferedReader "false" , ?

+5
7

ready()!=

ready() , . , read . , false, .

, , , readLine() null.

String line = in.readLine();
while(line != null){
   ...
   line = in.readLine();
}
+7

, in.ready(), - :

while ((nextLine = in.readLine()) != null) {
  result += nextLine;
}

, . in.ready().

+4

, - , -. - :

while ((String nextLine = in.readLine()) != null) {
    //System.out.println(nextLine);
    result += nextLine;
}

, , null, . . :

http://download.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html#readLine()

+3

BufferedReader.ready() :

Reader.ready() javadoc :

[] true, read() , false . , false , .

BufferedReader.ready() javadoc :

, . , , .

, , BufferedReader.ready() false , . , ready() .

+2

, - , "" . URL.openURLStream() , OP. HTTP, HTTPS-.

 URL  getURL = new URL (servletURL.toString() + identifier+"?"+key+"="+value);      
 URLConnection uConn = getURL.openConnection();

 BufferedReader br = new BufferedReader (new
                            InputStreamReader (uConn.getInputStream()));
 for (String s = br.readLine() ; s != null ; s = br.readLine()) {
      System.out.println ("[ServletOut] " + s);
      // do stuff with s
 }               
 br.close();
0

BufferedReader.ready() , .... , .

, , , . , , ....

0

in.ready(), :

    for (int i = 0; i < 10; i++) {
        System.out.println("is InputStreamReader ready: " + in.ready());
        if (!in.ready()) {
            Thread.sleep(1000);
        } else {
            break;
        }
    }
0

All Articles