习惯了读写字符串时用PrintWriter和BufferedReader
PrintWriter(BufferedWriter(OutputStreamWriter(OutputStream)))
BufferedReader(InputStreamReader(InputStream))
这两天给别人看项目代码的时候看到程序里用了readUTF和writeUTF这两个方法,顿时从尘封的记忆中唤起DataInput和DataOutput这两个接口。很久不用,我都快把它俩给忘了。
项目运行的时候总是会抛出EOFException,不明白怎么回事,于是看了看JDK文档,自己写了一段小程序测试了一下,猜测readUTF方法似乎是通过抛出EOFException来表明到达数据的末尾,如同bufferedReader.readLine()通过返回null表明到达数据的结尾一样,只是表达的方式不同而已。因此只需要通过检查EOFException异常来结束程序即可。
EOFException文档中提到:
此异常主要被数据输入流用来表明到达流的末尾。注意,其他许多输入操作返回一个特殊值表示到达流的末尾,而不是抛出异常。
以下是我写的测试代码:(Java6)
服务端:
import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;public class Client { /** * @param args */ public static void main(String[] args) { Socket socket = null; DataInputStream in = null; DataOutputStream out = null; try { socket = new Socket("localhost", 10011); out = new DataOutputStream(socket.getOutputStream()); in = new DataInputStream(socket.getInputStream()); for (int i = 0; i < 100; i++) { out.writeUTF("aaa1"); out.flush(); System.out.println(in.readUTF()); } in.close(); out.close(); socket.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
客户端:
import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.EOFException;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;public class Server { static class ReadTask implements Runnable { private final Socket so; public ReadTask(Socket so) { this.so = so; } @Override public void run() { DataInputStream in = null; DataOutputStream out = null; try { in = new DataInputStream(this.so.getInputStream()); out = new DataOutputStream(this.so.getOutputStream()); System.out.println("开始处理"); while (true) { String cmd = in.readUTF(); // do some work TimeUnit.MILLISECONDS.sleep(10); out.writeUTF("result -- " + cmd); } } catch (EOFException e) { System.out.println("处理完成"); } catch (IOException e1) { e1.printStackTrace(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } if (this.so != null) { try { this.so.close(); } catch (IOException e) { e.printStackTrace(); } } } } } /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { ExecutorService exec = Executors.newCachedThreadPool(); ServerSocket server = null; try { server = new ServerSocket(10011); System.out.println("server started"); while (true) { Socket socket = server.accept(); ReadTask task = new ReadTask(socket); exec.execute(task); } } catch (IOException e) { e.printStackTrace(); } finally { exec.shutdown(); exec.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); if (server != null) { try { server.close(); } catch (IOException e) { e.printStackTrace(); } } } }}