PrintWriter用法案例

这个东西用的比较少,今天看书时遇到了,整理一下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18

public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = new ServerSocket(7000);
    Socket clientSocket = serverSocket.accept();

    BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    PrintWriter pw = new PrintWriter(clientSocket.getOutputStream(), true);

    String request, response;
    while ((request = br.readLine()) != null) {
        if ("Done".equals(request)) {
            break;
        }
        response = request;
        pw.println(response);
    }
}