Scanner类(util包)

Scanner类是一个不可变的类,实现了迭代器接口。一个简单的文本扫描器,可以使用正则表达式解析原始类型和字符串。

  • 扫描用户输入
 // 这段代码只做个用法示例,按字符类型输入
Scanner sc =new Scanner(System.in);
System.out.println("请输入:");
if(sc.hasNext()){
//(不能得到带有空格的字符串)
String s = sc.next(); // String 类型
System.out.println("字符串:"+ s);
}
if(sc.hasNextInt()){ // int 类型
int s = sc.nextInt();
System.out.println("整型:" + s);
}
if(sc.hasNextLong()){ // long 类型
long s = sc.nextLong();
System.out.println("长整型:" + s);
}
if(sc.hasNextFloat()){ // float 类型
float s = sc.nextFloat();
System.out.println("浮点型:" + s);
}
if(sc.hasNextDouble()){ // double 类型
double s = sc.nextDouble();
System.out.println("双精度型:" + s);
}
if(sc.hasNextLine()){ // String 类型
String s = sc.nextLine();
System.out.println("整行字符串:" + s);
}
if(sc.hasNextBoolean()){ // boolean 类型
boolean s = sc.nextBoolean();
System.out.println("布尔类型:" + s);
}
System.out.println("-------结束-----");
  • 扫描文件里的数据

    Scanner sc = new Scanner(new File("E:\\zz.txt"));

  • 通过输入流获取

 Scanner s = new Scanner(new FileInputStream("E:\\zz.txt")); // 扫描文件输入流中的数据
/* while(s.next().equals("123")){
System.out.println(s.next()); // 打印"123"后一个字符串
}*/
while (s.hasNext(Pattern.compile("\\d+"))){
System.out.println(s.next()); // 打印匹配到的字符串
}
  • 使用正则解析字符串
 String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input);
s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
MatchResult result = s.match();
for (int i=1; i<=result.groupCount(); i++)
System.out.println(result.group(i));
s.close();

System类(lang包)

System也是一个不可变类,是一些与系统相关的属性和方法且都是静态的。包括标准输入,标准输出和错误输出流; 访问外部定义的属性和环境变量; 一种加载文件和库的方法。

// 相关属性
System.err.println("System.err 标准错误流");
System.out.println("System.out 标准输出流");
System.out.println("System.in 标准输入流"); // 相关方法
String [] s = {"ab", "cd", "ef", "gh"};
String [] s1 = new String[6];
System.arraycopy(s,0, s1,2,4);
System.out.println(Arrays.toString(s)); // [ab, cd, ef, gh]
System.out.println(Arrays.toString(s1)); // [null, null, ab, cd, ef, gh] System.out.println(System.getenv());// 返回当前系统环境的不可修改的字符串映射视图(Map<String,String>类型)
System.out.println(System.getenv("Java_home")); // G:\Java\jdk1.8.0_161 获取指定环境变量的值
// 以下方法还有对应的set方法
System.out.println(System.getProperties()); // 返回当前的系统相关属性。
System.out.println(System.getProperty("os.name")); // 返回当前操作系统的名称 Windows 10 System.out.println(System.nanoTime());// 返回正在运行的JVM的高分辨率时间源的当前值(以纳秒为单位)
System.out.println(System.currentTimeMillis()); // 返回当前时间(以毫秒为单位) System.out.println("====="+System.lineSeparator()); // 返回与系统相关的行分隔符字符串 System.load("C:\\Drivers\\Audio.Realtek\\HDA\\New\\AERTAC64.dll"); // 指定到文件
System.loadLibrary("lib"); // 底层实现 Runtime.getRuntime().loadLibrary(name)
System.exit(0); // 终止当前运行的Java虚拟机,非 0 的状态码表示异常终止
System.gc(); // 运行垃圾回收器

实际上out,err 的就是PrintStream类型的输出流, in是InputStream类型输入流

 System.out.println("This is a test");
System.err.println("print to console");
try {
byte[] b = new byte[1024];
System.setIn(new FileInputStream("E:\\test1.txt"));
int len = System.in.read(b);
System.out.println("file context: "+ new String(b, 0, len)); // 打印test1.txt内容 System.setErr(new PrintStream("E:\\test2.txt"));
System.setOut(new PrintStream("E:\\test3.txt"));
} catch (IOException e) {
e.printStackTrace();
}
System.err.println("input to test2.txt"); // 内容输出到test2.txt 文件
System.out.println("input to test3.txt"); // 内容输出到test3.txt 文件

Java_Scanner和System类的更多相关文章

  1. java中的System类

    System类代表系统,系统级的很多属性和控制方法都放置在该类的内部.该类位于java.lang包. 由于该类的构造方法是private的,所以无法创建该类的对象,也就是无法实例化该类.其内部的成员变 ...

  2. System类和Random类

    System类 成员方法: public static void gc():运行垃圾回收器 public static void exit(int status):退出垃圾回收器 public sta ...

  3. Java api 入门教程 之 JAVA的SYSTEM类

    System类代表系统,系统级的很多属性和控制方法都放置在该类的内部.该类位于java.lang包. 由于该类的构造方法是private的,所以无法创建该类的对象,也就是无法实例化该类.其内部的成员变 ...

  4. System类

    System类是一些与系统相关属性和方法的集合,而且System类中所有的属性都是静态的,要想引用这些属性和方法,直接使用System类调用即可. //======================== ...

  5. java 的SYSTEM类【转】

    java 的SYSTEM类[转] Posted on 2009-12-03 16:46 火之光 阅读(728) 评论(0) 编辑 收藏 System类代表系统,系统级的很多属性和控制方法都放置在该类的 ...

  6. java 14 -5 System类

    System类包含一些有用的类字段和方法.它不能被实例化. 方法: 1.public static void gc():运行垃圾回收器. 2.public static void exit(int s ...

  7. System类及其常用函数

    System 类包含一些有用的类字段和方法.它不能被实例化. 常用方法: 1.static void arraycopy(Object src, int srcPos, Object dest, in ...

  8. Java API —— System类

    1.System类概述         System 类包含一些有用的类字段和方法.它不能被实例化.  2.成员方法         public static void gc():运行垃圾回收器   ...

  9. Java System类看到的一点小记

    System类 位置java.lang包中 是final类,不能被继承,不能被修改 ,不能被实例化 private System(){}私有的构造函数,不允许被其他对象进行实例化 public fin ...

随机推荐

  1. SpringBoot返回date日期格式化,解决返回为TIMESTAMP时间戳格式或8小时时间差

    问题描述 在Spring Boot项目中,使用@RestController注解,返回的java对象中若含有date类型的属性,则默认输出为TIMESTAMP时间戳格式 ,如下所示: 解决方案    ...

  2. CDH 集群机器上部署 Jupyter notebook 使用 Pyspark 读取 Hive 数据库

    开始直接在 CDH Pyspark 的环境里面运行 Ipython . spark = SparkSession \ .builder \ .master('yarn') \ .appName('md ...

  3. Ajax提交表单初接触

    <!doctype html> <html class="no-js"> <head> <meta charset="utf-8 ...

  4. LODOP设置打印设计返回JS代码是变量

    前面有一篇博文是介绍JS模版的加载和赋值,赋值有两种,详细可查看本博客的那篇博文:LodopJS代码模版的加载和赋值简单来说,就是打印项的值是变量,在添加打印项前进行赋值:打印项的值是字符串,给打印项 ...

  5. 基于JavaCv并发读取本地视频流并提取每帧32位dhash特征

    1.读取本地视频流,pom依赖 依赖于 org.bytedeco下的javacv/opencv/ffmpeg 包 <dependency> <groupId>org.byted ...

  6. [洛谷P1357] 花园

    题目类型:状压\(DP\) -> 矩阵乘法 绝妙然而思维难度极其大的一道好题! 传送门:>Here< 题意:有一个环形花圃,可以种两种花:0或1. 要求任意相邻的\(M\)个花中1的 ...

  7. Transaction check error: file /etc/rpm/macros.ghc-srpm from install of redhat-rpm-config-9.1.0-80.el7.centos.noarch conflicts with file from package epel-release-6-8.noarch Error Summary ----------

    ./certbot-auto certonly 报错: Transaction check error:   file /etc/rpm/macros.ghc-srpm from install of ...

  8. Maven pom文件标签解析大全

    <span style="padding:0px; margin:0px"><project xmlns="http://maven.apache.or ...

  9. springboot+mybatis+cucumber

    import org.junit.runner.RunWith; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucu ...

  10. spring定时任务详解(@Scheduled注解)

    Spring配置文件xmlns加入 xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocati ...