有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java致命错误:1:1:文件过早结束。读取url时出错

你好,我正在尝试以一定的间隔读取URL提要,并将其存储为对象。然而,当我试图运行它时,它给了我这个错误[致命错误]:1:1:文件过早结束

以下是代码: 此URL是一个静态http地址

url = new URL(thisUrl);
URLstream = url.openStream();
ir = new InputStreamReader(URLstream);
buff = new BufferedReader(ir);
String xObject = "";

while (buff.ready()) {
     String temp = buff.readLine();
     xObject += temp;
}

使用流后,我将关闭它

URLstream.close();
ir.close();
buff.close();

共 (1) 个答案

  1. # 1 楼答案

    为ready()方法的返回类型和值引用文档

    True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.

    你的下一个read()可能会阻塞流。这并不意味着你已经看完了这条流。用这个代替

    String temp = null;
    while ( (temp = buff.readLine()) != null) {
         xObject += temp;
    }