在Java中,有多少種方法可以讀取鍵盤輸入的資料?
**java.io**包提供了各種類,用於從各種來源讀取和寫入資料到各種目標。
您可以使用各種類(例如,Scanner、BufferedReader、InputStreamReader、Console等)從使用者(鍵盤)讀取資料。
使用Scanner類
從Java 1.5開始引入Scanner類。此類接受File、InputStream、Path和String物件,使用正則表示式逐個標記讀取所有基本資料型別和字串(來自給定來源)。預設情況下,空格被視為分隔符(用於將資料分成標記)。
要從鍵盤讀取資料,您需要使用標準輸入作為來源(System.in)。對於每種資料型別,都提供了nextXXX()方法,例如nextInt()、nextShort()、nextFloat()、nextLong()、nextBigDecimal()、nextBigInteger()、nextLong()、nextShort()、nextDouble()、nextByte()、nextFloat()、next()。
示例
下面的Java程式使用Scanner類從使用者讀取資料。
import java.util.Scanner;
class Student{
String name;
int age;
float percent;
boolean isLocal;
char grade;
Student(String name, int age, float percent, boolean isLocal, char grade){
this.name = name;
this.age = age;
this.percent = percent;
this.isLocal = isLocal;
this.grade = grade;
}
public void displayDetails(){
System.out.println("Details..............");
System.out.println("Name: "+this.name);
System.out.println("Age: "+this.age);
System.out.println("Percent: "+this.percent);
if(this.isLocal) {
System.out.println("Nationality: Indian");
}else {
System.out.println("Nationality: Foreigner");
}
System.out.println("Grade: "+this.grade);
}
}
public class ReadData {
public static void main(String args[]) {
Scanner sc =new Scanner(System.in);
System.out.println("Enter your name: ");
String name = sc.next();
System.out.println("Enter your age: ");
int age = sc.nextInt();
System.out.println("Enter your percent: ");
float percent = sc.nextFloat();
System.out.println("Are you local (enter true or false): ");
boolean isLocal = sc.nextBoolean();
System.out.println("Enter your grade(enter A, or, B or, C or, D): ");
char grade = sc.next().toCharArray()[0];
Student std = new Student(name, age, percent, isLocal, grade);
std.displayDetails();
}
}輸出
Enter your name: Krishna Enter your age: 25 Enter your percent: 86 Are you local (enter true or false): true Enter your grade(enter A, or, B or, C or, D): A Details.............. Name: Krishna Age: 25 Percent: 86.0 Nationality: Indian Grade: A
使用BufferedReader
Java的BufferedReader類用於從指定來源(字元輸入流)讀取字元流。此類的建構函式接受InputStream物件作為引數,您可以傳遞一個**InputStreamReader**。
示例
下面的Java程式使用**BufferedReader**類從使用者讀取資料。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Student{
String name;
int age;
float percent;
boolean isLocal;
char grade;
Student(String name, int age, float percent, boolean isLocal, char grade){
this.name = name;
this.age = age;
this.percent = percent;
this.isLocal = isLocal;
this.grade = grade;
}
public void displayDetails(){
System.out.println("Details..............");
System.out.println("Name: "+this.name);
System.out.println("Age: "+this.age);
System.out.println("Percent: "+this.percent);
if(this.isLocal) {
System.out.println("Nationality: Indian");
}else {
System.out.println("Nationality: Foreigner");
}
System.out.println("Grade: "+this.grade);
}
}
public class ReadData {
public static void main(String args[]) throws IOException {
BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your name: ");
String name = reader.readLine();
System.out.println("Enter your age: ");
int age = Integer.parseInt(reader.readLine());
System.out.println("Enter your percent: ");
float percent = Float.parseFloat(reader.readLine());
System.out.println("Are you local (enter true or false): ");
boolean isLocal = Boolean.parseBoolean(reader.readLine());
System.out.println("Enter your grade(enter A, or, B or, C or, D): ");
char grade = (char) reader.read();
Student std = new Student(name, age, percent, isLocal, grade);
std.displayDetails();
}
}輸出
Enter your name: Krishna Enter your age: 25 Enter your percent: 86 Are you local (enter true or false): true Enter your grade(enter A, or, B or, C or, D): A Details.............. Name: Krishna Age: 25 Percent: 86.0 Nationality: Indian Grade: A
使用Console類
此類用於與控制檯(鍵盤/螢幕)裝置寫入/讀取資料。它提供了一個**readLine()**方法,該方法從鍵盤讀取一行。您可以使用**console()**方法獲取Console類的物件。
**注意** - 如果您嘗試在非互動式環境(如IDE)中執行此程式,它將無法工作。
示例
下面的Java程式使用**Console**類從使用者讀取資料。
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
class Student{
String name;
int age;
float percent;
boolean isLocal;
char grade;
Student(String name, int age, float percent, boolean isLocal, char grade){
this.name = name;
this.age = age;
this.percent = percent;
this.isLocal = isLocal;
this.grade = grade;
}
public void displayDetails(){
System.out.println("Details..............");
System.out.println("Name: "+this.name);
System.out.println("Age: "+this.age);
System.out.println("Percent: "+this.percent);
if(this.isLocal) {
System.out.println("Nationality: Indian");
}else {
System.out.println("Nationality: Foreigner");
}
System.out.println("Grade: "+this.grade);
}
}
public class ReadData {
public static void main(String args[]) throws IOException {
Console console = System.console();
if (console == null) {
System.out.println("Console is not supported");
System.exit(1);
}
System.out.println("Enter your name: ");
String name = console.readLine();
System.out.println("Enter your age: ");
int age = Integer.parseInt(console.readLine());
System.out.println("Enter your percent: ");
float percent = Float.parseFloat(console.readLine());
System.out.println("Are you local (enter true or false): ");
boolean isLocal = Boolean.parseBoolean(console.readLine());
System.out.println("Enter your grade(enter A, or, B or, C or, D): ");
char grade = console.readLine().toCharArray()[0];
Student std = new Student(name, age, percent, isLocal, grade);
std.displayDetails();
}
}輸出
Enter your name: Krishna Enter your age: 26 Enter your percent: 86 Are you local (enter true or false): true Enter your grade(enter A, or, B or, C or, D): A Details.............. Name: Krishna Age: 26 Percent: 86.0 Nationality: Indian Grade: A
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP