【JAVA IO】_Scanner筆記

來源:互聯網
上載者:User

【JAVA IO】_Scanner筆記

本章目標:
掌握Scanner類的作用
使用Scanner接收輸入資料

Scanner簡介:

此類不在java.io包中,而在java.util包中,此類是一個工具類

使用Scanner接收鍵盤的輸入資料:

import java.util.*;public class ScannerDemo01{    public static void main(String args[]){        Scanner scan = new Scanner(System.in);    //從鍵盤接收資料        System.out.println("輸入資料:");        String str = scan.next();    //接收資料        System.out.println("輸入的資料為:"+str);        }}

比直接使用BufferedReader更加方便。但是,以上的輸入資料代碼實際上是存在問題的。
如果輸入資料之間存在了空格,則會以空格為分隔字元。如果要想輸入空格的話,則就必須修改分隔字元,將分隔字元變成“\n”
public Scanner useDelimiter(String pattern)

修改分隔字元:

import java.util.* ;public class ScannerDemo02{    public static void main(String args[]){        Scanner scan = new Scanner(System.in) ;    // 從鍵盤接收資料        System.out.print("輸入資料:") ;        scan.useDelimiter("\n") ;        String str = scan.next() ;    // 接收資料        System.out.println("輸入的資料為:" + str) ;    }};

之前的輸入資料都是以字串的形式返回的,實際上使用Scanner類也可以方便的直接返回整數或者小數。
有以下的方法:
接收整數:
|-判斷是否是整數:public boolean hasNextInt(),判斷是否是整數,如果是則返回true
|-接收資料:public int nextInt()
接收小數:
|-判斷是否是小數:public boolean hasNextFloat()
|-接收資料:public float nextFloat()

import java.util.* ;public class ScannerDemo03{    public static void main(String args[]){        Scanner scan = new Scanner(System.in) ;    // 從鍵盤接收資料        int i = 0 ;        float f = 0.0f ;        System.out.print("輸入整數:") ;        if(scan.hasNextInt()){    // 判斷輸入的是否是整數            i = scan.nextInt() ;    // 接收整數            System.out.println("整數資料:" + i) ;        }else{            System.out.println("輸入的不是整數!") ;        }        System.out.print("輸入小數:") ;        if(scan.hasNextFloat()){    // 判斷輸入的是否是小數            f = scan.nextFloat() ;    // 接收小數            System.out.println("小數資料:" + f) ;        }else{            System.out.println("輸入的不是小數!") ;        }    }};

Scanner這個類雖然可以接收各種類型,但是對於日期型的資料卻無法接收。
如果要想接收Date類型的資料,則只能通過字串轉型,但是然接收的時候依然可以使用Scanner類中提供的方法進行驗證:
驗證:public String hasNext(String pattern)
接收:public String next(String pattern)

import java.util.* ;import java.text.* ;public class ScannerDemo04{    public static void main(String args[]){        Scanner scan = new Scanner(System.in) ;    // 從鍵盤接收資料        String str = null ;        Date date = null ;        System.out.print("輸入日期(yyyy-MM-dd):") ;        if(scan.hasNext("^\\d{4}-\\d{2}-\\d{2}$")){    // 驗證            str = scan.next("^\\d{4}-\\d{2}-\\d{2}$") ;    // 接收            try{                date = new SimpleDateFormat("yyyy-MM-dd").parse(str) ;            }catch(Exception e){}        }else{            System.out.println("輸入的日期格式錯誤!") ;        }        System.out.println(date) ;    }};

還可以直接從檔案中讀取資訊。
假設:d:\test.txt檔案
使用Scanner直接讀取檔案內容

import java.util.* ;import java.text.* ;import java.io.* ;public class ScannerDemo05{    public static void main(String args[]){        File f = new File("D:" + File.separator + "test.txt") ;    // 指定操作檔案        Scanner scan = null ;        try{            scan = new Scanner(f) ;    // 從鍵盤接收資料        }catch(Exception e){}        String str = null ;        while(scan.hasNext()){            str = scan.next();    //    取資料        }        System.out.println("檔案內容為:" + str) ;    }};

現在的檔案內容確實讀取進來,但是此程式依然不完美。以上只能讀取出第一行資料,因為存在換行,所以認為讀完了。

import java.util.* ;import java.text.* ;import java.io.* ;public class ScannerDemo05{    public static void main(String args[]){        File f = new File("D:" + File.separator + "test.txt") ;    // 指定操作檔案        Scanner scan = null ;        try{            scan = new Scanner(f) ;    // 從鍵盤接收資料        }catch(Exception e){}        StringBuffer str = new StringBuffer() ;        while(scan.hasNext()){            str.append(scan.next()).append('\n')    ;    //    取資料        }        System.out.println("檔案內容為:" + str) ;    }};

在使用Scanner讀檔案的時候,要考慮到換行的功能。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.