Java PrintWriter setError()方法及示例

PrintWriter类的setError()方法 (PrintWriter Class setError() method)

  • setError() method is available in java.io package.

    setError()方法在java.io包中可用。

  • setError() method is used to denote that an error has occurred. It will cause subsequent call of checkError() method to return true until clearError() is called.

    setError()方法用于表示已发生错误。 它将导致随后调用checkError()方法返回true,直到调用clearError()为止。

  • setError() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    setError()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • setError() method does not throw an exception at the time of set error state.

    setError()方法在设置错误状态时不会引发异常。

Syntax:

句法:

    protected void setError();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of the method is void, it returns nothing.

该方法的返回类型为void ,不返回任何内容。

Example:

例:

// Java program to demonstrate the example 
// of void setError() method of PrintWriter

import java.io.*;

public class SetErrorOfPW extends PrintWriter {
    public SetErrorOfPW(OutputStream os) {
        super(os);
    }

    public static void main(String[] args) {
        String str = "Java Programming";

        // Instantiates SetErrorOfPW
        SetErrorOfPW p_stm = new SetErrorOfPW(System.out);

        // Display str
        p_stm.println("str: " + str);

        // By using flush() method is to
        // flush the stream immediately
        p_stm.flush();
        System.out.println("Stream Flushed...");

        // By using setError() method is used to
        // set internal error state
        p_stm.setError();

        // By using close() method is to 
        // close the stream p_stm
        System.out.println("Stream Shutdown....");
        p_stm.close();
    }
}

Output

输出量

str: Java Programming
Stream Flushed...
Stream Shutdown....


翻译自: https://www.includehelp.com/java/printwriter-seterror-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现P2P聊天通信的步骤: 1. 创建一个服务器,用于客户端之间的通信。服务器需要监听客户端的连接请求,并将客户端的信息存储在一个列表中。 2. 创建一个客户端,用于与其他客户端进行通信。客户端需要向服务器注册自己的信息,包括唯一主键、IP地址和端口号。 3. 当客户端想要与其他客户端进行通信时,它需要向服务器查询目标客户端的信息,包括IP地址和端口号。 4. 客户端通过目标客户端的IP地址和端口号连接到目标客户端,建立通信连接。 5. 一旦连接建立成功,客户端之间就可以通过套接字进行通信,发送和接收消息、图片、音频和视频等文件。 以下是Java实现P2P聊天通信的代码示例: 1. 服务器代码 ```java import java.io.*; import java.net.*; import java.util.*; public class Server { private static List<ClientInfo> clients = new ArrayList<ClientInfo>(); public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("Server started."); while (true) { Socket socket = serverSocket.accept(); System.out.println("New client connected: " + socket.getInetAddress().getHostAddress()); ClientInfo clientInfo = new ClientInfo(socket); clients.add(clientInfo); Thread thread = new Thread(new ServerHandler(clientInfo, clients)); thread.start(); } } catch (IOException e) { e.printStackTrace(); } } } class ServerHandler implements Runnable { private ClientInfo clientInfo; private List<ClientInfo> clients; public ServerHandler(ClientInfo clientInfo, List<ClientInfo> clients) { this.clientInfo = clientInfo; this.clients = clients; } public void run() { try { BufferedReader reader = new BufferedReader(new InputStreamReader(clientInfo.getSocket().getInputStream())); while (true) { String message = reader.readLine(); System.out.println("Received message from " + clientInfo.getId() + ": " + message); if (message.startsWith("REGISTER")) { String[] parts = message.split(" "); clientInfo.setId(parts[1]); clientInfo.setIp(clientInfo.getSocket().getInetAddress().getHostAddress()); clientInfo.setPort(Integer.parseInt(parts[2])); System.out.println("Registered client " + clientInfo.getId() + " at " + clientInfo.getIp() + ":" + clientInfo.getPort()); } else if (message.startsWith("QUERY")) { String[] parts = message.split(" "); String targetId = parts[1]; ClientInfo targetClient = null; for (ClientInfo client : clients) { if (client.getId().equals(targetId)) { targetClient = client; break; } } if (targetClient != null) { String response = "CONNECT " + targetClient.getIp() + " " + targetClient.getPort(); PrintWriter writer = new PrintWriter(clientInfo.getSocket().getOutputStream()); writer.println(response); writer.flush(); System.out.println("Sent response to " + clientInfo.getId() + ": " + response); } else { String response = "ERROR Target client not found."; PrintWriter writer = new PrintWriter(clientInfo.getSocket().getOutputStream()); writer.println(response); writer.flush(); System.out.println("Sent response to " + clientInfo.getId() + ": " + response); } } else { // Forward message to target client String[] parts = message.split(" "); String targetId = parts[0]; ClientInfo targetClient = null; for (ClientInfo client : clients) { if (client.getId().equals(targetId)) { targetClient = client; break; } } if (targetClient != null) { PrintWriter writer = new PrintWriter(targetClient.getSocket().getOutputStream()); writer.println(message); writer.flush(); System.out.println("Forwarded message from " + clientInfo.getId() + " to " + targetClient.getId() + ": " + message); } else { String response = "ERROR Target client not found."; PrintWriter writer = new PrintWriter(clientInfo.getSocket().getOutputStream()); writer.println(response); writer.flush(); System.out.println("Sent response to " + clientInfo.getId() + ": " + response); } } } } catch (IOException e) { e.printStackTrace(); } } } class ClientInfo { private String id; private String ip; private int port; private Socket socket; public ClientInfo(Socket socket) { this.socket = socket; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public Socket getSocket() { return socket; } } ``` 2. 客户端代码 ```java import java.io.*; import java.net.*; public class Client { private static String id; private static String ip; private static int port; public static void main(String[] args) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter your ID: "); id = reader.readLine(); Socket socket = new Socket("localhost", 8888); System.out.println("Connected to server."); PrintWriter writer = new PrintWriter(socket.getOutputStream()); writer.println("REGISTER " + id + " 0"); writer.flush(); BufferedReader serverReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String response = serverReader.readLine(); System.out.println("Received response from server: " + response); if (response.startsWith("CONNECT")) { String[] parts = response.split(" "); ip = parts[1]; port = Integer.parseInt(parts[2]); System.out.println("Connected to client " + id + " at " + ip + ":" + port); Thread thread = new Thread(new ClientHandler(socket)); thread.start(); while (true) { String message = reader.readLine(); Socket targetSocket = new Socket(ip, port); PrintWriter targetWriter = new PrintWriter(targetSocket.getOutputStream()); targetWriter.println(id + " " + message); targetWriter.flush(); targetSocket.close(); } } else { System.out.println("Failed to connect to client " + id); } } catch (IOException e) { e.printStackTrace(); } } } class ClientHandler implements Runnable { private Socket socket; public ClientHandler(Socket socket) { this.socket = socket; } public void run() { try { BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (true) { String message = reader.readLine(); System.out.println("Received message: " + message); } } catch (IOException e) { e.printStackTrace(); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值