Java_Scanner和System类

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:Javajdk1.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 文件
原文地址:https://www.cnblogs.com/zeo-to-one/p/9398915.html