电脑
Scanner是SDK1.5新增的一个类,可是使用该类创建一个对象.Scanner reader=new Scanner(System.in); 然后reader对象调用下列方法(函数),读取用户在命令行输入的各种数据类型 next.Byte(),nextDouble(),nextFloat,nextInt(),nextLine(),nextLong(),nextShot() 上述方法执行时都会造成堵塞,等待用户在命令行输入数据回车确认.例如,拥护在键盘输入12.34,hasNextFloat()的值是true,而hasNextInt()的值是false. NextLine()等待用户输入一个文本行并且回车,该方法得到一个String类型的数据。
C:\java>另一个例子,读取并分析文本文件:hrinfo.txt,文本文件的内容如下:老赵,28,feb-01,true小竹,22,dec-03,false阿波,21,dec-03,false凯子,25,dec-03,true 程序: import java.util.Scanner;import java.io.File;import java.io.FileNotFoundException;public class readhuman {private static void readfile(String filename) {try { Scanner scanner = new Scanner(new File(filename)); scanner.useDelimiter(System.getProperty('line.separator')); while (scanner.hasNext()) { parseline(scanner.next()); } scanner.close();}catch (FileNotFoundException e) { System.out.println(e);}}private static void parseline(String line) { Scanner linescanner = new Scanner(line); linescanner.www.gzlij.com useDelimiter(','); //可以修改usedelimiter参数以读取不同分隔符分隔的内容 String name = linescanner.next(); int age = linescanner.nextInt(); String idate = linescanner.next(); boolean iscertified = linescanner.nextBoolean(); System.out.println('姓名:'+name+' ,年龄:'+ age+' ,入司时间:'+ idate+' ,验证标记:'+iscertified );}public static void main(String[] args) { if (args.length != 1) { System.err.println('usage: java readhuman file location'); System.exit(0); } readfile(args[0]);}}运行结果:C:\java>java readhuman hrinfo.txt姓名:老赵 ,年龄:28 ,入司时间:feb-01 ,验证标记:true姓名:小竹 ,年龄:22 ,入司时间:dec-03 ,验证标记:false姓名:阿波 ,年龄:21 ,入司时间:dec-03 ,验证标记:false姓名:凯子,年龄:25 ,入司时间:dec-03 ,验证标记:true