public class t10_6s {
DatagramSocket dsock = null;
DatagramPacket inp;
DatagramPacket outp;
byte[] inbuff=new byte[100];
byte[] outbuff;
PrintWriter pw;
String s,s1;
public t10_6s(){
try {
dsock=new DatagramSocket (8000);
pw=new PrintWriter(new FileWriter("test_6.txt"));
while(true){
inp=new DatagramPacket(inbuff,inbuff.length );
dsock.receive(inp);
s=new String(inp.getData(),0,inp.getLength());
System.out.println(":"+s);
pw.print( s);
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
finally{pw.close();}
}
public static void main(String[] args) {
new t10_6s();
}
}
问题:当程序关闭是怎样把文件流(pw)关闭!!!
----------------解决方案--------------------------------------------------------
addwindowsListener(new windowsAdapter(){
public void windowsClosing(WindowsEvent e){
xxx.close();
}
})
不过是图形界面里面的关闭程序时候调用的 你可以参考以下
----------------解决方案--------------------------------------------------------
顶楼上的
----------------解决方案--------------------------------------------------------