Reading a web page in Java IOException Premature EOF

后端 未结 5 633
离开以前
离开以前 2020-12-16 19:10

I am frequently getting a \'Premature EOF\' Exception when reading a web page.

The following is the StackTrace

java.io.IOException: Premature EOF
            


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 19:30

    This may be because you are reading the content line by line and for the last line the file may be missing a return, to signal the end of line. Replace your while with this:

    int BUFFER_SIZE=1024;
    char[] buffer = new char[BUFFER_SIZE]; // or some other size, 
    int charsRead = 0;
    while ( (charsRead  = rd.read(buffer, 0, BUFFER_SIZE)) != -1) {
      sb.append(buffer, 0, charsRead);
    }
    

提交回复
热议问题