JAVA的三種標準輸入方式舉例

import java.io.*;
import java.util.*;
public class TestInput {


    public TestInput() {   /無參數的構造方法
    }


     //第一種方式:字節輸入流
    public static void main(String[] args) throws IOException {
    byte b[]= new byte[50];                    //用於存放輸入的字節數組
    System.out.println("please input :");
    int n=System.in.read(b);                //接收輸入的數據到字符數組b中並存入數組的字節數到n中
    String s = new String(b,0,n);            //將字節數組轉換爲字符串
    System.out.println("You Input:"+s);


   //第二種方式:以字節輸入流讀入,存放在字符緩衝區中
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));   //從輸入流接收數據存放在字符       緩衝區br中

    String s1=br.readLine();            //讀取一行
    System.out.println("You input:"+s1);



   //第三種方式:以字節輸入流讀入,存放在掃描器中

    Scanner read =new Scanner(System.in);      //以字節輸入流讀入,存放在掃描器中
    while(read.hasNext()){          // 如果掃描到下一個字符
    String s2 = read.next();           //則讀取下一個字符
    System.out.println("You input:"+s2);            //掃描器
 }    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章