牢记buffered需要刷新flush
package loadfile; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) throws UnknownHostException, IOException { // TODO Auto-generated method stub Socket s = new Socket("192.168.2.143",10003); BufferedReader br = new BufferedReader(new FileReader("client.txt")); BufferedWriter bwout = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); int ch = 0; while((ch = br.read()) != -1) { bwout.write(ch); bwout.flush(); } s.shutdownOutput(); BufferedReader brin = new BufferedReader(new InputStreamReader(s.getInputStream())); String line = null; line = brin.readLine(); System.out.println(line); br.close(); s.close(); bwout.close(); brin.close(); } } package loadfile; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub ServerSocket ss = new ServerSocket(10003); Socket s = ss.accept(); BufferedReader brin = new BufferedReader(new InputStreamReader(s.getInputStream())); BufferedWriter brw = new BufferedWriter(new FileWriter("server.txt")); String line = null; while((line = brin.readLine()) != null) { brw.write(line); brw.newLine(); brw.flush(); } PrintWriter out = new PrintWriter(s.getOutputStream(),true); out.write("上传成功"); ss.close(); out.close(); brin.close(); brw.close(); } }
原文地址:https://www.cnblogs.com/WINDZLY/p/11925239.html
时间: 2024-10-04 07:41:14