我有一部分代码需要服务器发送,然后再从客户端接收一些输入。问题是,如果我关闭PrintWriter的流,则在声明BufferReader时它将自动进入catch状态。我无法删除PrintWriter的out.close,因为在客户端,我有一个循环,在该循环中等待未估计数量的字符串,因此,通过关闭流来告诉服务器服务器已完成操作系统的唯一方法(我猜测)。这是代码:
PrintWriter out = new PrintWriter(incoming.getOutputStream(), true);
for (int j = 0; j < i; j++) {
out.println(tmp[j]); // send the strings to the client
}
out.close();
BufferedReader in;
String op;
boolean exit = false;
try {
in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
while ((op = in.readLine()) != null) {
System.out.println("OP: " + op);
if (op.equals("Elimina")) {
String tmp = in.readLine();
contenutoTextArea.append("Ho eliminato la mail ").append(tmp).append(" \n");
textarea.setText(contenutoTextArea.toString());
File file = new File("src/server/" + nomeAccount + "/" + tmp + ".txt");
file.delete();
}
}
} catch (IOException ex) {
System.out.println("Unable to read messages");
(代码在in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
处停止工作,并处于catch状态)