Scanner类的方法定义:
· 构造方法:public Scanner(InputStream source);
· 设置读取分割符:public Scanner useDelimiter(String pattern);
· 判断是否有数据:public boolean hasNextXxx();
· 取出数据:public 数据类型 nextXxx();
1 package cn.demo; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.util.Scanner; 6 7 public class Test { 8 public static void main(String[] args) throws Exception { 9 Scanner scan = new Scanner(new FileInputStream(new File("e:" + File.separator +"ly" +File.separator+ "TestOne.java"))); 10 scan.useDelimiter(" "); 11 while(scan.hasNext()){ 12 System.out.println(scan.next()); 13 } 14 scan.close(); 15 } 16 }
总结:程序输入数据使用Scanner(如果Scanner不好用了使用BufferedReader)。