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.hqwc.cn/news/164843.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

wpf添加Halcon的窗口控件报错:下列控件已成功添加到工具箱中,但未在活动设计器中启用

报错截图如下&#xff1a; 注意一下新建工程的时候选择wpf应用而不是wpf应用程序。 添加成功的控件&#xff1a;

C++常用格式化输出转换

在C语言中可以用printf以一定的格式打印字符&#xff0c;C当然也可以。 输入输出及命名空间还不太了解的小伙伴可以看一看C入门讲解第一篇。  在C中&#xff0c;可以用流操作符&#xff08;stream manipulators&#xff09;控制数据的输出格式&#xff0c;这些流操作符定义在2…

大语言模型(LLM)综述(七):大语言模型设计应用与未来方向

A Survey of Large Language Models 前言8 A PRACTICAL GUIDEBOOK OF PROMPT DESIGN8.1 提示创建8.2 结果与分析 9 APPLICATIONS10 CONCLUSION AND FUTURE DIRECTIONS 前言 随着人工智能和机器学习领域的迅速发展&#xff0c;语言模型已经从简单的词袋模型&#xff08;Bag-of-…

Workbench环境中常见问题

问题描述&#xff1a;1 解决方案&#xff1a;2 问题描述&#xff1a;在WB中启动Fluent&#xff0c;报错&#xff0c;提示 “The requested operatjon requires elevation” 解决方案&#xff1a;这个问题是因为WB主程序没有管理员权限导致。使用管理员权限启动WB后&#xff0c…

【达梦数据库】mysql与达梦整数类型对比关系

最近遇了mysql 和达梦整数类型的数据范围对比&#xff0c;做了个表格供大家分享 对比表格 要说明的是我整理的时候&#xff0c;达梦貌似没有无符号整数类型&#xff08;防杠保护&#xff09;&#xff0c;也就是只能将mysql/dm 的有符号整数类型的的范围值进行对比 MYSQL - t…

HK WEB3 MONTH Polkadot Hong Kong 火热报名中!

HK Web3 Month 11月除了香港金融科技周外&#xff0c;HK Web3 Month又是一大盛事&#xff0c;从10月29日开始开幕直到11月18日结束。此次将齐聚世界各地的Web3产业从业者、开发者、社群成员和学生来参与本次盛会。除外&#xff0c;超过75位产业知名的讲者与超过50场工作坊将为…

Docker Stack部署应用详解+Tomcat项目部署详细实战

Docker Stack 部署应用 概述 单机模式下&#xff0c;可以使用 Docker Compose 来编排多个服务。Docker Swarm 只能实现对单个服务的简单部署。而Docker Stack 只需对已有的 docker-compose.yml 配置文件稍加改造就可以完成 Docker 集群环境下的多服务编排。 stack是一组共享…

sql学习笔记(三)

目录 1.四舍五入 2.向上取整 3.向下取整 4.Hive 分区 5.case when条件语句 6.日期函数 7.字符串函数 8.窗口函数 1️⃣排序函数 1.四舍五入 round select round(3.14) —>3 2.向上取整 ceiling select ceiling(12.15) —>13 3.向下取整 floor select floor(12…

【JavaSE】基础笔记 - 类和对象(下)

目录 1、this引用 1.1、为什么要有this引用 1.2、什么是this引用 1.3、 this引用的特性 2、 对象的构造及初始化 2.1、 如何初始化对象 2.2、构造方法 2.2.1、概念 2.2.2、特性 2.3、默认初始化 2.4、就地初始化 上篇&#xff1a;【JavaSE】基础笔记 - 类和对象&#…

Tkinter创建菜单窗口

一、说明 本文专门记录如何添加Tkinter的菜单&#xff0c;包括&#xff0c;菜单如何生成&#xff0c;菜单如何布局&#xff0c;菜单如何绑定回调函数&#xff0c;并安排代码示例&#xff0c;凡期望做菜单界面的读者&#xff0c;可以全面参考。 二、创建菜单实现步骤 2.1 总体…

适合汽车应用的MAX49017ATA/VY、MAX40025AAWT、MAX40025CAWT、MAX40026ATA/VY(线性)微功耗比较器

一、MAX49017ATA/VY 内置基准电压源的1.7V、双通道微功耗比较器 MAX49017是一款节省空间的双通道比较器&#xff0c;内置基准电压源&#xff0c;提供推挽输出。该器件通过了AEC-Q100认证&#xff0c;非常适合汽车应用&#xff0c;例如汽车电池监控系统、信息娱乐系统音响主机和…

一文带你看懂交直流一体化电源系统调试步骤

交直流一体化电源系统 交直流一体化电源系统由交流电源系统、直流电源系统、逆变电源系统、通信电源系统、一体化监控系统组成。它是将交流/直流电源系统、逆变电源系统、通信电源系统统一设计、监控、生产、调试、服务的电源产品&#xff0c;是在智能交流、直流综合供电系统的…