集册 Java实例教程 在任何Java应用程序中接收控制台输入

在任何Java应用程序中接收控制台输入

欢马劈雪     最近更新时间:2020-01-02 10:19:05

412
在任何Java应用程序中接收控制台输入

import java.io.BufferedInputStream;/**n  o  w  j  a  v  a . c o m**/

import java.io.IOException;


public class Main {

  public static String readLine() {

    StringBuilder response = new StringBuilder();

    try (BufferedInputStream buff = new BufferedInputStream(System.in)) {


      int in;

      char inChar;

      do {

        in = buff.read();

        inChar = (char) in;

        if ((in != -1) & (in != '\n') & (in != '\r')) {

          response.append(inChar);/*来自 时 代 J a v a 公 众 号 - nowjava.com*/

        }

      } while ((in != -1) & (inChar != '\n') & (in != '\r'));

      buff.close();

      return response.toString();

    } catch (IOException e) {

      System.out.println("Exception: " + e.getMessage());

      return null;

    }

  }


  public static 
展开阅读全文