Java流(Stream)、Scanner类

  • 读取控制台输入

Java 的控制台输入由 System.in 完成。

为了获得一个绑定到控制台的字符流,你可以把 System.in 包装在一个 BufferedReader 对象中来创建一个字符流。

创建BufferedReader的基本语法:

BufferedReader br = new BufferedReader(new 
                      InputStreamReader(System.in));

BufferedReader 对象创建后,我们便可以使用 read() 方法从控制台读取一个字符,或者用 readLine() 方法读取一个字符串。

从 BufferedReader 对象读取一个字符要使用 read() 方法:

int read( ) throws IOException

每次调用 read() 方法,它从输入流读取一个字符并把该字符作为整数值返回。 当流结束的时候返回 -1。该方法抛出 IOException。

read()方法示例:

//使用BufferReader 在控制台读取字符

import java.io.*;

public class BRRead {

  public static void main(String []args) throws IOException

{

  char c;

  //使用System.in创建BufferedReader

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  System.out.println("输入字符, 按下 'q' 键退出。");

  //读取字符

  do{

    c = (char) br.read();

    System.out.println(c);

    }while(c != 'q');

  }

}

  •  从控制台读取字符串

一般格式为:

String readLine( ) throws IOException
// 使用 BufferedReader 在控制台读取字符
import java.io.*;
public class BRReadLines {
  public static void main(String args[]) throws IOException
  {
    // 使用 System.in 创建 BufferedReader 
    BufferedReader br = new BufferedReader(new
                            InputStreamReader(System.in));
    String str;
    System.out.println("Enter lines of text.");
    System.out.println("Enter 'end' to quit.");
    do {
       str = br.readLine();
       System.out.println(str);
    } while(!str.equals("end"));
  }
}
  • 控制台的输出

在此前已经介绍过,控制台的输出由 print( ) 和 println() 完成。这些方法都由类 PrintStream 定义,System.out 是该类对象的一个引用。

PrintStream 继承了 OutputStream类,并且实现了方法 write()。这样,write() 也可以用来往控制台写操作。

PrintStream 定义 write() 的最简单格式如下所示:

void write(int byteval)

该方法将 byteval 的低八位字节写到流中。

import java.io.*;
 
// 演示 System.out.write().
public class WriteDemo {
   public static void main(String args[]) {
      int b; 
      b = 'A';
      System.out.write(b);
      System.out.write('
');
   }
}
  • Scanner类

java.util.Scanner 是 Java5 的新特征,我们可以通过 Scanner 类来获取用户的输入。

创建Scanner类的基本语法:

Scanner s = new Scanner(System.in);

Scanner类通过 next() 与 nextLine() 方法获取输入的字符串,

在读取前可以使用 hasNext 与 hasNextLine 判断是否还有输入的数据。

示例:next()

import java.util.Scanner; 
 
public class ScannerDemo {  
    public static void main(String[] args) {  
        Scanner scan = new Scanner(System.in); 
    // 从键盘接收数据  
 
    //next方式接收字符串
        System.out.println("next方式接收:");
        // 判断是否还有输入
        if(scan.hasNext()){   
          String str1 = scan.next();
          System.out.println("输入的数据为:"+str1);  
        }  
 
    }  
}

输出:

next方式接收:
Jeffrey Lu
输入的数据为:Jeffrey

示例:nextLine()

import java.util.Scanner; 
 
public class ScannerDemo {  
    public static void main(String[] args) {  
        Scanner scan = new Scanner(System.in); 
    // 从键盘接收数据  
 
    //nextLine方式接收字符串
        System.out.println("nextLine方式接收:");
        // 判断是否还有输入
        if(scan.hasNextLine()){   
          String str2 = scan.nextLine();
          System.out.println("输入的数据为:"+str2);  
        }  
 
    }  
}

输出:

nextLine方式接收:
Jeffrey Lu
输入的数据为:Jeffrey Lu

next() 与 nextLine() 区别

next():

  • 1、一定要读取到有效字符后才可以结束输入。
  • 2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
  • 3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。 
  • next() 不能得到带有空格的字符串。

nextLine(): 

  • 1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。 
  • 2、可以获得空白。

如果要输入 int 或 float 类型的数据,在 Scanner 类中也有支持,但是在输入之前最好先使用 hasNextXxx() 方法进行验证,再使用 nextXxx() 来读取。

原文地址:https://www.cnblogs.com/dear_diary/p/6526978.html