Java InputStreamReader ready()方法与示例

InputStreamReader类ready()方法 (InputStreamReader Class ready() method)

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

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

  • ready() method is used to check whether this InputStreamReader is ready to be ready or not.

    ready()方法用于检查此InputStreamReader是否准备就绪。

  • ready() 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.

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

  • ready() method may throw an exception at the time of checking the state of the stream.

    在检查流的状态时, ready()方法可能会引发异常。

    IOException: This exception may throw when getting any input/output error while performing.

    IOException :在执行过程中遇到任何输入/输出错误时,可能引发此异常。

Syntax:

句法:

    public boolean ready();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of the method is String, it returns true when this stream is ready to be read otherwise it returns false.

该方法的返回类型为String ,当此流准备好被读取时返回true,否则返回false。

Example:

例:

// Java program to demonstrate the example 
// of boolean ready() method of 
// InputStreamReader

import java.io.*;
public class ReadyOfISR {
 public static void main(String[] args) throws Exception {
  InputStream is_stm = null;
  InputStreamReader isr_stm = null;
  int val = 0;

  try {
   // Instantiates FileInputStream and InputStreamReader 
   is_stm = new FileInputStream("D:\\includehelp.txt");
   isr_stm = new InputStreamReader(is_stm);

   // By using ready() method is to         
   // check whether this stream isr_stm
   // stream is ready to be read or not

   boolean status = isr_stm.ready();
   System.out.println("isr_stm.ready(): " + status);

  } catch (Exception ex) {
   System.out.println(ex.toString());

  } finally {

   // with the help of this block is to
   // free all necessary resources linked
   // with the stream

   if (is_stm != null) {
    is_stm.close();

    if (isr_stm != null) {
     isr_stm.close();
    }
   }
  }
 }
}

Output

输出量

isr_stm.ready(): true


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Java代码示例,用于实现一个SMTP服务器程序,可以接收来自邮件客户端的邮件,然后转发到目标邮件服务器的用户邮箱: ```java import java.io.*; import java.net.*; public class SMTPServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; boolean listening = true; try { // 创建ServerSocket对象并绑定到SMTP默认端口25 serverSocket = new ServerSocket(25); System.out.println("SMTPServer started on port 25..."); } catch (IOException e) { System.err.println("Could not listen on port 25."); System.exit(-1); } while (listening) { new SMTPServerThread(serverSocket.accept()).start(); } serverSocket.close(); } } class SMTPServerThread extends Thread { private Socket socket = null; public SMTPServerThread(Socket socket) { super("SMTPServerThread"); this.socket = socket; } public void run() { try { PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String inputLine; String outputLine; // 发送SMTP服务器欢迎消息 outputLine = "220 SMTPServer ready"; out.println(outputLine); // 处理客户端SMTP请求 while ((inputLine = in.readLine()) != null) { System.out.println("Received: " + inputLine); if (inputLine.toUpperCase().startsWith("HELO")) { // 处理HELO命令 outputLine = "250 Hello"; out.println(outputLine); } else if (inputLine.toUpperCase().startsWith("MAIL FROM")) { // 处理MAIL FROM命令 outputLine = "250 OK"; out.println(outputLine); } else if (inputLine.toUpperCase().startsWith("RCPT TO")) { // 处理RCPT TO命令 outputLine = "250 OK"; out.println(outputLine); } else if (inputLine.toUpperCase().startsWith("DATA")) { // 处理DATA命令,接收邮件内容并转发到目标邮件服务器 outputLine = "354 End data with <CR><LF>.<CR><LF>"; out.println(outputLine); StringBuilder sb = new StringBuilder(); while ((inputLine = in.readLine()) != null) { if (inputLine.equals(".")) { break; } sb.append(inputLine); } System.out.println("Received mail: " + sb.toString()); // 将邮件内容转发到目标邮件服务器 // TODO: 实现邮件转发功能 outputLine = "250 OK"; out.println(outputLine); } else if (inputLine.toUpperCase().startsWith("QUIT")) { // 处理QUIT命令,关闭连接 outputLine = "221 Goodbye"; out.println(outputLine); break; } else { // 处理未知命令 outputLine = "500 Command unrecognized"; out.println(outputLine); } } out.close(); in.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 这个示例代码实现了一个最基础的SMTP服务器框架,可以处理客户端发送的HELO、MAIL FROM、RCPT TO、DATA和QUIT等SMTP命令,并接收和转发邮件内容。具体实现需要根据具体情况进行调整和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值