简体   繁体   English

为什么readLine()有效,而readUTF()总是抛出IOException

[英]why readLine() works, and readUTF() always throws IOException

I send a post request to an address, and this address will return a xml format data back. 我将发帖请求发送到一个地址,该地址将返回xml格式的数据。

I can print out the data by readLine(), but when i use readUTF(), it always throws IOException . 我可以通过readLine(),打印出数据readLine(),但是当我使用readUTF(),它总是抛出IOException below is the code 下面是代码

DataInputStream input = new DataInputStream(urlCon.getInputStream());
String inputLine = "";

if((inputLine = input.readUTF()) != null) {
    System.out.println(inputLine.toString());
}
input.close();

why readUTF() does not work? 为什么readUTF()不起作用? is it because the data is xml format? 是因为数据是xml格式?

If you're reading lines, use readLine() . 如果要读取行,请使用readLine() Data to be read with readUTF() must have been written with writeUTF(). readUTF()读取的数据必须已经用writeUTF().写入writeUTF(). See the Javadoc. 请参阅Javadoc。

NB readUTF() doesn't return null, so testing for it is futile. 注意readUTF()不返回null,因此对其进行测试是徒劳的。

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

相关问题 readUTF工作一次,然后抛出EOFException - readUTF works once, then throws EOFException BufferedReader.readLine()始终抛出IOException,但仅在真正的智能手机上 - BufferedReader.readLine() always throws IOException but only with a real smartphone 为什么Java StringReader会抛出IOException? - Why Java StringReader throws IOException? 为什么必须使用“throws IOException” - Why it is mandatory to use “throws IOException” 为什么ByteArrayOutputStream.close()抛出IOException? - Why ByteArrayOutputStream.close() throws IOException? 为什么这个pojo程序“ readline”总是返回null? - Why this pojo program “readline” always return null? 为什么我总是收到IOException? - Why am I always getting a IOException? ObjectInputStream 在重复使用 readUTF() 后抛出 EOFException - ObjectInputStream throws EOFException after repeated use of readUTF() Java doPost()在使用getParameter()时有效,但是doGet()总是抛出null……为什么? - Java doPost() works when using getParameter(),but doGet() always throws null… WHY? 为什么只有特定情况才需要在方法签名中抛出 IOException? - Why throws for IOException in method signature is needed for only specific cases?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM