• Scanner类


    其实我们比较常用的是采用Scanner类来进行数据输入,下面来给一个Scanner的例子吧。

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

    【例子1】

    
    import java.util.Scanner;
    
    /**  
     * Scanner的小例子,从键盘读数据  
     *   
     */  
    
    public class ScannerDemo{
    
        public static void main(String[] args){
    
            Scanner sca = new Scanner(System.in);
    
            // 读一个整数
    
            int temp = sca.nextInt();
    
            System.out.println(temp);
    
            //读取浮点数
    
            float flo=sca.nextFloat();
    
            System.out.println(flo);
    
            //读取字符
    
            //...等等的,都是一些太基础的,就不师范了。
    
        }
    
    }
    

    其实Scanner可以接受任何的输入流

    下面给一个使用Scanner类从文件中读出内容

    【例子2】

    
    import java.io.File;  
    import java.io.FileNotFoundException;  
    import java.util.Scanner;   
    
    
    /**  
     * Scanner的小例子,从文件中读内容  
     */  
    
    public class ScannerDemo{
    
        public static void main(String[] args){
    
            File file = new File("d:" + File.separator + "hello.txt");
    
            Scanner sca = null;
    
            try{
    
                sca = new Scanner(file);
    
            }catch(FileNotFoundException e){
    
                e.printStackTrace();
    
            }
    
            String str = sca.next();
    
            System.out.println("从文件中读取的内容是:" + str);
    
        }
    
    }
    

    【运行结果】:

    从文件中读取的内容是:这些文件中的内容哦!

  • 相关阅读:
    JS 语法: document.getElementById没有括号
    c#,WinForm中读写配置文件App.config
    System.Data.SQLite数据库简介
    把JS 脚本嵌入CS运行
    Syteline Receiving By Purchase Orders Report
    TSQL DATEPART() Functions
    TSQL CONVERT Functions
    TSQL CAST Functions
    接口(Interface)替代抽象(Abstract)
    独立子查询
  • 原文地址:https://www.cnblogs.com/yuyu666/p/9733907.html
Copyright © 2020-2023  润新知