当前位置: 首页 > news >正文

Java基础-015-System.java常用类

Java基础-015-System.java常用类

  • 1、标准输入输出
  • 2、获取属性
  • 3、System.java初始化
  • 4、设置标准输出System.out

java/lang/System.java


1、标准输入输出

System.inSystem.out

public class Test {public static void main(String[] args) {String charsetName = String.valueOf(Charset.defaultCharset());System.out.println("System.out.println : charsetName="+charsetName);Scanner s = new Scanner(System.in,"gbk");if (s.hasNext()) {String str1 = s.next();System.out.println("输入的数据为:" + str1);}}
}

2、获取属性

jdk/internal/util/SystemProps.javajava/lang/VersionProps.javajdk/internal/util/StaticProperty.java

System.out.println("System.getProperty(\"sun.jnu.encoding\")="+System.getProperty("sun.jnu.encoding"));
System.out.println("System.getProperty(\"native.encoding\")="+System.getProperty("native.encoding"));
System.out.println("System.getProperty(\"sun.stdout.encoding\")="+System.getProperty("sun.stdout.encoding"));
System.out.println("System.getProperty(\"user.dir\")="+System.getProperty("user.dir"));
System.out.println("System.getProperties()="+System.getProperties());
System.getProperties()={java.specification.version=18, sun.cpu.isalist=amd64, sun.jnu.encoding=GBK, java.class.path=D:\IdeaProjects\Jcmd\build\classes\java\main;D:\IdeaProjects\Jcmd\build\resources\main;D:\gradle\envs\caches\modules-2\files-2.1\com.gitee.xhbruce.xhLog\xLog\1.0.3\8734bf5379ee7cee34cf10ab875570208b2360ca\xLog-1.0.3.jar;D:\gradle\envs\caches\modules-2\files-2.1\org.apache.commons\commons-exec\1.3\8dfb9facd0830a27b1b5f29f84593f0aeee7773b\commons-exec-1.3.jar, java.vm.vendor=Oracle Corporation, sun.arch.data.model=64, user.variant=, java.vendor.url=https://java.oracle.com/, java.vm.specification.version=18, os.name=Windows 10, user.country=CN, sun.java.launcher=SUN_STANDARD, sun.boot.library.path=D:\ProgramFiles\Java\jdk-18.0.1.1\bin, sun.java.command=org.xhbruce.Test, jdk.debug=release, sun.cpu.endian=little, user.home=C:\Users\Administrator, user.language=zh, java.specification.vendor=Oracle Corporation, java.version.date=2022-04-22, java.home=D:\ProgramFiles\Java\jdk-18.0.1.1, file.separator=\, java.vm.compressedOopsMode=Zero based, line.separator=
, java.vm.specification.vendor=Oracle Corporation, java.specification.name=Java Platform API Specification, user.script=, sun.management.compiler=HotSpot 64-Bit Tiered Compilers, java.runtime.version=18.0.1.1+2-6, user.name=xhbruce, path.separator=;, os.version=10.0, java.runtime.name=Java(TM) SE Runtime Environment, file.encoding=UTF-8, java.vm.name=Java HotSpot(TM) 64-Bit Server VM, java.vendor.url.bug=https://bugreport.java.com/bugreport/, java.io.tmpdir=C:\Users\ADMINI~1\AppData\Local\Temp\, java.version=18.0.1.1, user.dir=D:\IdeaProjects\Jcmd, os.arch=amd64, java.vm.specification.name=Java Virtual Machine Specification, sun.os.patch.level=, native.encoding=GBK, java.library.path=D:\ProgramFiles\Java\jdk-18.0.1.1\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\ProgramData\chocolatey\bin;D:\Books\Linux\bat;D:\AndroidStudio\Sdk\tools;D:\AndroidStudio\Sdk\platform-tools;D:\AndroidStudio\Sdk\cmake\3.6.4111459\bin;D:\AndroidStudio;D:\ProgramFiles\Java\Openjfx\javafx-sdk-21\lib;D:\ProgramFiles\Java\jdk-18.0.1.1\bin;D:\ProgramFiles\Java\jdk-18.0.1.1\jre;C:\Program Files\mingw64\bin;%CTAGS_HOME%;%TOMCAT_HOME%\bin;C:\Program Files\nodejs\;C:\Program Files\Git\cmd;D:\ProgramFiles\ffmpeg\bin;D:\gradle\envs;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\Users\Administrator\AppData\Roaming\npm;G:\ProgramFiles\JetBrains\Toolbox\scripts;D:\Program Files\Microsoft VS Code\bin;C:\Users\Administrator\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts;;., java.vm.info=mixed mode, sharing, java.vendor=Oracle Corporation, java.vm.version=18.0.1.1+2-6, sun.io.unicode.encoding=UnicodeLittle, java.class.version=62.0}
属性字段功能获取结果
java.specification.version18
sun.cpu.isalistamd64
sun.jnu.encodingGBK
java.class.pathD:\IdeaProjects\Jcmd\build\classes\java\main;…
java.vm.vendorOracle Corporation
sun.arch.data.model64
user.variant
java.vendor.urlhttps://java.oracle.com/
java.vm.specification.version18
os.name系统名称Windows 10
user.country系统国家CN
sun.java.launcherSUN_STANDARD
sun.boot.library.pathD:\ProgramFiles\Java\jdk-18.0.1.1\bin
sun.java.commandorg.xhbruce.Test
jdk.debugrelease
sun.cpu.endianlittle
user.home系统用户文件夹C:\Users\Administrator
user.language系统语言zh
java.specification.vendorOracle Corporation
java.version.datejava版本日期2022-04-22
java.homejava路径D:\ProgramFiles\Java\jdk-18.0.1.1
file.separator文件分隔符\
java.vm.compressedOopsModeZero based
line.separator
java.vm.specification.vendorOracle Corporation
java.specification.nameJava Platform API Specification
user.script
sun.management.compilerHotSpot 64-Bit Tiered Compilers
java.runtime.version18.0.1.1+2-6
user.namexhbruce
path.separator;
os.version10.0
java.runtime.nameJava™ SE Runtime Environment
file.encodingUTF-8
java.vm.nameJava HotSpot™ 64-Bit Server VM
java.vendor.url.bughttps://bugreport.java.com/bugreport/
java.io.tmpdirC:\Users\ADMINI~1\AppData\Local\Temp, java.version=18.0.1.1
user.dir当前路径D:\IdeaProjects\Jcmd
os.archamd64
java.vm.specification.nameJava Virtual Machine Specification
sun.os.patch.level
native.encodingGBK
java.library.path系统环境配置pathD:\ProgramFiles\Java\jdk-18.0.1.1\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;…
java.vm.infomixed mode, sharing
java.vendorOracle Corporation
java.vm.version18.0.1.1+2-6
sun.io.unicode.encodingUnicodeLittle
java.class.version62.0

3、System.java初始化

  • SystemProps.initProperties()VersionProps.init(tempProps)VM.saveProperties(tempProps)StaticProperty.javaHome()系统属性和java相关属性初始化
  • setIn0setOut0setErr0标准输出输出配置

在这里插入图片描述

    /*** Initialize the system class.  Called after thread initialization.*/private static void initPhase1() {// register the shared secrets - do this first, since SystemProps.initProperties// might initialize CharsetDecoders that rely on itsetJavaLangAccess();// VM might invoke JNU_NewStringPlatform() to set those encoding// sensitive properties (user.home, user.name, boot.class.path, etc.)// during "props" initialization.// The charset is initialized in System.c and does not depend on the Properties.Map<String, String> tempProps = SystemProps.initProperties();VersionProps.init(tempProps);// There are certain system configurations that may be controlled by// VM options such as the maximum amount of direct memory and// Integer cache size used to support the object identity semantics// of autoboxing.  Typically, the library will obtain these values// from the properties set by the VM.  If the properties are for// internal implementation use only, these properties should be// masked from the system properties.//// Save a private copy of the system properties object that// can only be accessed by the internal implementation.VM.saveProperties(tempProps);props = createProperties(tempProps);// Check if sun.jnu.encoding is supported. If not, replace it with UTF-8.var jnuEncoding = props.getProperty("sun.jnu.encoding");if (jnuEncoding == null || !Charset.isSupported(jnuEncoding)) {notSupportedJnuEncoding = jnuEncoding == null ? "null" : jnuEncoding;props.setProperty("sun.jnu.encoding", "UTF-8");}StaticProperty.javaHome();          // Load StaticProperty to cache the property valueslineSeparator = props.getProperty("line.separator");FileInputStream fdIn = new FileInputStream(FileDescriptor.in);FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);setIn0(new BufferedInputStream(fdIn));// sun.stdout/err.encoding are set when the VM is associated with the terminal,// thus they are equivalent to Console.charset(), otherwise the encoding// defaults to native.encodingsetOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding", StaticProperty.nativeEncoding())));setErr0(newPrintStream(fdErr, props.getProperty("sun.stderr.encoding", StaticProperty.nativeEncoding())));// Setup Java signal handlers for HUP, TERM, and INT (where available).Terminator.setup();// Initialize any miscellaneous operating system settings that need to be// set for the class libraries. Currently this is no-op everywhere except// for Windows where the process-wide error mode is set before the java.io// classes are used.VM.initializeOSEnvironment();// The main thread is not added to its thread group in the same// way as other threads; we must do it ourselves here.Thread current = Thread.currentThread();current.getThreadGroup().add(current);// Subsystems that are invoked during initialization can invoke// VM.isBooted() in order to avoid doing things that should// wait until the VM is fully initialized. The initialization level// is incremented from 0 to 1 here to indicate the first phase of// initialization has completed.// IMPORTANT: Ensure that this remains the last initialization action!VM.initLevel(1);}

4、设置标准输出System.out

java/io/PrintStream.java

  • 主要用于IDE和系统终端输出设置编码Encoding
  • 设置输出到文件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

String stdoutEncoding = System.getProperty("file.encoding")
if (!System.out.charset().name().equals(stdoutEncoding)) {try {PrintStream ps = new PrintStream(System.out, true, stdoutEncoding);System.setOut(ps);} catch (UnsupportedEncodingException e) {throw new RuntimeException(e);}
}
System.out.println("#########################");
System.out.println("System.out" + System.out);
try {PrintStream ps = new PrintStream(new File(System.getProperty("user.dir")+"/test.txt"), stdoutEncoding);System.setOut(ps);
} catch (UnsupportedEncodingException | FileNotFoundException e) {throw new RuntimeException(e);
}
System.out.println("System.out" + System.out);
System.out.println("System.out.charset() = " + System.out.charset());
System.out.println("native.encoding = " + System.getProperty("native.encoding"));
System.out.println("sun.jnu.encoding = " + System.getProperty("sun.jnu.encoding"));
System.out.println("sun.stdout.encoding = " + System.getProperty("sun.stdout.encoding"));
System.out.println("sun.io.unicode.encoding = " + System.getProperty("sun.io.unicode.encoding"));
System.out.println("#########################");
System.out.println("你好 java");
XLog.d("你好 xhburce");
http://www.lryc.cn/news/219658.html

相关文章:

  • Flutter笔记:发布一个模块 scale_design - (移动端)设计师尺寸适配工具
  • torch增加维度操作
  • 软件测试面试题及答案2024
  • C现代方法(第18章)笔记——声明
  • Spring Data Redis + RabbitMQ - 基于 string + hash 实现缓存,计数(高内聚)
  • 【四、http】go的http的文件下载
  • Java web(六):FilterListenerAJAX
  • 初识jQuery
  • MATLAB算法实战应用案例精讲-【图像处理】计算机视觉(最终篇)
  • repo执行出现/usr/bin/env: ‘python’: No such file or directory问题
  • 算法模板之单调栈解密 | 图文详解
  • 187.重复的 DNA 序列
  • Sentinel黑白名单授权规则解读
  • Spring底层原理学习笔记--第二讲--(BeanFactory实现与ApplicaitonContext实现)
  • 云原生|kubernetes |kubelet服务加入系统守护进程supervisor(centos7系统下演示通过)
  • onnx 模型加载部署运行方式
  • 第68讲:MySQL触发器的核心概念以及常见的触发类型应用案例
  • VS Code 开发 Spring Boot 类型的项目
  • 数据中心加密:保障数据安全的重要一环
  • 分享90个节日庆典PPT,总有一款适合您
  • Python Faker批量生成测试数据
  • Docker-compose 运行MySQL 连接不上
  • Educational Codeforces Round 2 D 计算几何
  • hexo博客发布换电脑换地方了怎么办?
  • 最新知识付费变现小程序源码/独立后台知识付费小程序源码/修复登录接口
  • 奥威BI软件 | 职场人的数据可视化救星
  • 最长公共前缀[简单]
  • Java后端开发(十一)-- Mysql8的详细安装与环境配置
  • 什么是Spring?什么是IOC?什么是DI?IOC和DI的关系? —— 零基础可无压力学习,带源码
  • PyTorch 从tensor.grad 看 backward(权重参数) 和 gradient accumulated