首页 > 代码库 > java.util.Scanner简单应用

java.util.Scanner简单应用

import java.util.Scanner;import java.io.*;public class FileScannerTest{    public static void main(String args[]){//**************Scanner 的一般用//1.public Scanner(InputStream source),利用InputStream 对象进行构造              Scanner myScanner1 = new Scanner(System.in);       while(myScanner1.hasNextInt()){           int i=myScanner1.nextInt();           System.out.println(i);       }//2.public Scanner(File source)  throws FileNotFoundException , 利用File对象进行构造               try{          Scanner myScanner2 = new Scanner(new File("in.txt"));          while(myScanner2.hasNextInt()){             int i=myScanner2.nextInt();             System.out.println(i);          }        }catch(FileNotFoundException e){           System.out.println("该文件不存在!");        }//3.public Scanner(String str), 利用一个String对象进行构造               Scanner myScanner3 = new Scanner(new String("1 2 3 4 a f f 4 56"));//遇到非整数的地方hasNextInt()返回false       while(myScanner3.hasNextInt()){             int i=myScanner3.nextInt();             System.out.println(i);          }    }}