JAVA Scanner 类
程序员文章站
2022-06-10 08:57:53
...
Scanner 是一个新的操作类,是在java.util 包中提供的一个操作类,使用此类可以方便的完成输入流的输入操作:
package org.scannerdemo; import java.util.Scanner; public class ScannerDemo01 { /** * @param args */ @SuppressWarnings("resource") public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int i =0; if (scan.hasNextInt()){ i = scan.nextInt(); } System.out.println("i:=" +i); } }
package org.scannerdemo; import java.util.Scanner; public class ScannerDemo02 { /** * @param args */ @SuppressWarnings("resource") public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); String str = ""; if (scan.hasNext()){ str = scan.next(); } System.out.println("str:=" +str); } }
支持正则匹配:
package org.scannerdemo; import java.util.Scanner; public class ScannerDemo04 { /** * @param args */ @SuppressWarnings("resource") public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); String str = ""; if (scan.hasNext("\\d{4}-\\d{2}-\\d{2}")){ str = scan.next(); } System.out.println("str:=" +str); } }
支持 InputSteam:
package org.scannerdemo; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class ScannerDemo03 { /** * @param args * @throws FileNotFoundException */ @SuppressWarnings("resource") public static void main(String[] args) throws FileNotFoundException { // TODO Auto-generated method stub File file = new File ("d:\\1.txt"); Scanner scan = new Scanner(new FileInputStream(file)); scan.useDelimiter("\n"); StringBuffer buf = new StringBuffer(); while (scan.hasNext()){ buf.append(scan.next()).append("\n"); } System.out.println(buf); } }
上一篇: 白手起家创业网有哪些,互联网创业如何成功
推荐阅读
-
PHP实现导出excel数据的类库用法
-
字符串的输入 及 String类 -
PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载
-
关于php 基于redis计数器类的详解
-
ant编译java报“非法字符: \65279 ”错误的解决方法 z
-
Java的参数传递是「按值传递」还是「按引用传递」?
-
安装ssl证书后报错Caused by: java.io.IOException: DerInputStream.getLength(): lengthTag=109, too big.
-
java获取文件的mime,java获取文件是不是文本,java获取文件类型(非后缀方式)
-
快速应对面试--分门别类--5.查找表-哈希
-
【JAVA设计模式】- 建造者模式