使用DataInputStream时Java中字节周围的输入格式错误

时间:2019-04-12 11:22:36

标签: java datainputstream

我有一个接受inputstream并将其打印的代码。当我使用Index Question Answer Proof Q1 question one wording Answer part 1 Proof part 1 Q1 question one wording Answer part 2 Proof part 2 Q2 question two wording Answer part 1 Proof part 1 Q2 question two wording Answer part 2 Proof part 2 时,代码在控制台中打印流,但是当我使用BufferedReader时,出现以下错误:

DataInputStream

代码如下:

Exception in thread "main" java.io.UTFDataFormatException: malformed input around byte 6351
    at java.io.DataInputStream.readUTF(DataInputStream.java:656)
    at java.io.DataInputStream.readUTF(DataInputStream.java:564)
    at Test.main(Test.java:14)

另外,当我将public class Test { public static void main(String[] args) throws IOException { Socket socket = new Socket("host", port); DataInputStream in = null; // BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); in = new DataInputStream(socket.getInputStream()); while(true){ // System.out.println(bufferedReader.readLine()); System.out.println(in.readUTF()); } } } readLine()一起使用时,DataInputStream显示它已被弃用。最后,我需要读取流并将其逐行传递给另一个方法,如下所示:

intellij

我得到同样的错误。我该如何纠正错误?

1 个答案:

答案 0 :(得分:0)

可能为时已晚,但这是一个答案。 在以下代码中,您不应编写:

Socket socket = new Socket("host", port);

套接字需要IP地址而不是名称。 例如:

Socket socket = new Socket("192.168.0.87", port);