本帖最后由 kakasa 于 2014-9-29 15:02 编辑
class Client {
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.1.102",10000);
BufferedReader br=new BufferedReader(new FileReader("D:\\test.txt"));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
BufferedReader FBbr=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
while((line=br.readLine())!=null)
{
out.println(line);
}
System.out.println(FBbr.readLine());
br.close();
FBbr.close();
s.close();
}
} class Server {
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10000);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println("connect to>>"+ip);
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(new FileWriter("D:\\test_copy.txt"),true);
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
String line=null;
while((line=br.readLine())!=null)
{
pw.println(line);
out.println("Upload Success");/*这个语句放在while循环内,不加结束标记程序可以正常复制文件并退出,为什么?
如果放在外面,毕老师说readLine方法还在等读取,为什么?*/
}
br.close();
ss.close();
}
} |