过于PrintWriter类的pintln()与write()方法的疑惑,求解中

fred127 2007-06-30 05:40:46
各位Ja友,下面的代码中TCPserverA类中的pw.println(date);如果换成pw.write(date.toString());后为什么客户端的控制台就不输出东西了?我已经用System.out.println()方法让它显示在控制台了呀,望解答,谢谢

import java.net.*;
import java.io.*;

public class TCPServerA {
public static void main(String[] args) {
ServerSocket ss=null;
Socket s=null;
PrintWriter pw=null;
try {
ss=new ServerSocket(9999);
while(true){
System.out.println("Server");
s=ss.accept();
OutputStream ois=s.getOutputStream();
pw=new PrintWriter(ois);
java.util.Date date=new java.util.Date();
pw.println(date);
//pw.write(date.toString());
pw.flush();
System.out.println(s.getInetAddress()+": "+date);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(pw!=null)pw.close();
if(s!=null)try{s.close();}catch(Exception e){}
if(ss!=null)try{ss.close();}catch(Exception e){}

}
}

}

import java.net.*;
import java.io.*;

public class TCPclientA {
public static void main(String[] args) {
Socket s=null;
BufferedReader br=null;
try {
s=new Socket("127.0.0.1",9999);
InputStream is=s.getInputStream();
br=new BufferedReader(new InputStreamReader(is));
String str=br.readLine();
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(s!=null)try{s.close();}catch(Exception e){}
if(br!=null)try{br.close();}catch(Exception e){}
}
}

}
...全文
177 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧