java.io.EOFException: 在java.io.DataInputStream.readUTF(Unknown Source)中 – 按行写入文件

huangapple go评论79阅读模式
英文:

java.io.EOFException: at java.io.DataInputStream.readUTF(Unknown Source) - write file line by line

问题

I made a simple Server-Client application to read line by line file from Server and write line by line in a new file in the client side. The application writes corretcly the lines in the file , but when the file reaches the end, I catch this error:

java.io.EOFException
	at java.io.DataInputStream.readUnsignedShort(Unknown Source)
	at java.io.DataInputStream.readUTF(Unknown Source)
	at java.io.DataInputStream.readUTF(Unknown Source)
	at it.sendfile.socket.FileClient1.main(FileClient1.java:19)

my code:

public class FileServer1 {
    public static void main(String[] args) {
        int port = 3000;
        // open server socket
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(port);
            Socket s = ss.accept();
            DataOutputStream outToClient = new DataOutputStream(s.getOutputStream());

            File testFile = new File("C:\\temp\\tmpbgoutcoge");
            BufferedReader input = new BufferedReader(new FileReader(testFile));
            String line;
            while ((line = input.readLine()) != null) {
                outToClient.writeUTF(line);
                outToClient.writeUTF("-----------------------------");
                outToClient.flush();
                Thread.currentThread().sleep(1000);
            }
            input.close();
            outToClient.close();
            s.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

public class FileClient1 {
    public static void main(String[] args) {
        try (PrintWriter out = new PrintWriter(new FileWriter("C:\\temp\\tmpbgoutcoge1.txt", true));) {
            Socket s = new Socket("localhost", 3000);
            DataInputStream inFromServer = new DataInputStream(new BufferedInputStream(s.getInputStream()));

            while (true) {
                String line = inFromServer.readUTF();
                System.out.println(line);
                out.write(line);
                out.println("\n");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

I have to find a While stop condition when I reach the end of file, but I've tryied with (line = inFromServer.readUTF()) != null but it's not worked.
In the FileClient1 class, I can't close the socket and the DataInputStream. A message says: "Unreachable code".

英文:

I made a simple Server-Client application to read line by line file from Server and write line by line in a new file in the client side. The application writes corretcly the lines in the file , but when the file reaches the end, I catch this error:

java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at it.sendfile.socket.FileClient1.main(FileClient1.java:19)

my code:

public class FileServer1 {
    public static void main(String[] args) {
        int port = 3000;
        // open server socket
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(port);
            Socket s = ss.accept();
            DataOutputStream outToClient = new DataOutputStream(s.getOutputStream());

            File testFile = new File("C:\\temp\\tmpbgoutcoge");
            BufferedReader input = new BufferedReader(new FileReader(testFile));
            String line;
            while ((line = input.readLine()) != null) {
                outToClient.writeUTF(line);
                outToClient.writeUTF("-----------------------------");
                outToClient.flush();
                Thread.currentThread().sleep(1000);
            }
            input.close();
            outToClient.close();
            s.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

public class FileClient1 {
    public static void main(String[] args) {
        try (PrintWriter out = new PrintWriter(new FileWriter("C:\\temp\\tmpbgoutcoge1.txt", true));) {
            Socket s = new Socket("localhost", 3000);
            DataInputStream inFromServer = new DataInputStream(new BufferedInputStream(s.getInputStream()));

            while (true) {
                String line = inFromServer.readUTF();
                System.out.println(line);
                out.write(line);
                out.println("\n");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

I have to find a While stop condition when I reach the end of file, but I've tryied with (line = inFromServer.readUTF())!= null but it's not worked.
In the FileClient1 class, I can't close the socket and the DataInputStream. A message says:"Unreachable code".

答案1

得分: 0

readUTF()永远不会返回null。在关闭之前,您需要发送一个“哨兵值” - 一个特殊的值,表示流的结束。

显然,您希望它是在正常使用情况下不太可能发送的内容。我建议使用"\uffff"(一个长度为1的字符串),因为U+FFFF保证不是一个已定义的字符。

英文:

readUTF() will never return null. You need to send a “sentinel value” before closing—a special value that indicates the end of the stream.

Obviously you want it to be something that is very unlikely to be sent in a normal use case. I suggest "\uffff" (a String with length 1), since U+FFFF is guaranteed not to be a defined character.

答案2

得分: -1

你可以使用以下代码替代原来的代码:

while (inFromServer.available() > 0) {  
    String line = inFromServer.readUTF();
    System.out.println(line);
    out.write(line);
    out.println("\n");
}
英文:

You can use

 while(inFromServer.available()>0) {  
String line = inFromServer.readUTF();
System.out.println(line);
out.write(line);
out.println("\n");
}  

instead of

        while (true) {
String line = inFromServer.readUTF();
System.out.println(line);
out.write(line);
out.println("\n");
}

huangapple
  • 本文由 发表于 2020年7月29日 20:09:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63153275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定