//scanner是接受system.in输入流的操作类 //scanner同时也支持文件输入流的操作 //一个可以使用正则表达式来分析基本类型和字符串的简单文本扫描器 Scanner scan = new Scanner(System.in); String str = null; if( scan.hasNext() ) { str = scan.next(); } System.out.println("str = "+ str); //正则扫描 Scanner scan = new Scanner(System.in); String str = null; if( scan.hasNext("\d{4}-\d{2}-\d{2}") ) { str = scan.next(); } System.out.println("str = " + str); //文件流 File file = new File("F:"+File.separator+"work"+File.separator+"work"+File.separator+"demo.txt"); Scanner scan = new Scanner( new FileInputStream(file) ); //设置扫描器分隔符,如以行为分隔符 scan.useDelimiter(" "); StringBuffer buf = new StringBuffer(); while( scan.hasNext() ) { buf.append(scan.next()).append(" "); } System.out.println(" str = " + buf);
//scanner是接受system.in输入流的操作类
//scanner同时也支持文件输入流的操作
//一个可以使用正则表达式来分析基本类型和字符串的简单文本扫描器