博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在Java中获取系统属性?
阅读量:2536 次
发布时间:2019-05-11

本文共 6239 字,大约阅读时间需要 20 分钟。

The  maintains a set of properties. These properties are stored in the form of key/value pairs. Both keys and values are Strings that define traits or attributes of the current working environment.

的维护一组属性。 这些属性以键/值对的形式存储。 键和值都是定义当前工作环境的特征或属性的字符串。

There are two methods that you can use to read the system properties: getProperty() and getProperties().

您可以使用两种方法来读取系统属性: getProperty()和getProperties()。

用Java获取所有系统属性 (Getting All System Properties in Java )

System.getProperties() returns an Enumeration of all the system properties. The following code prints all the system properties on the console.

System.getProperties()返回所有系统属性的枚举。 以下代码在控制台上打印所有系统属性。

import java.util.Enumeration;import java.util.Properties;public class Main {    public static void main(String[] args)    {        Properties p = System.getProperties();        Enumeration keys = p.keys();        while (keys.hasMoreElements()) {            String key = (String)keys.nextElement();            String value = (String)p.get(key);            System.out.println(key + ": " + value);        }    }}
System properties
Output
输出量

The Output in text form:

文本形式的输出:

gopherProxySet: falseawt.toolkit: sun.lwawt.macosx.LWCToolkitjava.specification.version: 11sun.cpu.isalist: sun.jnu.encoding: UTF-8java.class.path: /Users/jayant/Desktop/java/JD1/out/production/JD1java.vm.vendor: Oracle Corporationsun.arch.data.model: 64java.vendor.url: http://java.oracle.com/user.timezone: os.name: Mac OS Xjava.vm.specification.version: 11sun.java.launcher: SUN_STANDARDuser.country: GBsun.boot.library.path: /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/libsun.java.command: Mainhttp.nonProxyHosts: local|*.local|169.254/16|*.169.254/16jdk.debug: releasesun.cpu.endian: littleuser.home: /Users/jayantuser.language: enjava.specification.vendor: Oracle Corporationjava.version.date: 2018-10-16java.home: /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Homefile.separator: /java.vm.compressedOopsMode: Zero basedline.separator: java.specification.name: Java Platform API Specificationjava.vm.specification.vendor: Oracle Corporationjava.awt.graphicsenv: sun.awt.CGraphicsEnvironmentsun.management.compiler: HotSpot 64-Bit Tiered Compilersftp.nonProxyHosts: local|*.local|169.254/16|*.169.254/16java.runtime.version: 11.0.2+7-LTSuser.name: jayantpath.separator: :os.version: 10.14.2java.runtime.name: Java(TM) SE Runtime Environmentfile.encoding: UTF-8java.vm.name: Java HotSpot(TM) 64-Bit Server VMjava.vendor.version: 18.9java.vendor.url.bug: http://bugreport.java.com/bugreport/java.io.tmpdir: /var/folders/56/fc29wjz520x_21fmrl9r2jgc0000gn/T/java.version: 11.0.2user.dir: /Users/jayant/Desktop/java/JD1os.arch: x86_64java.vm.specification.name: Java Virtual Machine Specificationjava.awt.printerjob: sun.lwawt.macosx.CPrinterJobsun.os.patch.level: unknownjava.library.path: /Users/jayant/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.java.vendor: Oracle Corporationjava.vm.info: mixed modejava.vm.version: 11.0.2+7-LTSsun.io.unicode.encoding: UnicodeBigjava.class.version: 55.0socksNonProxyHosts: local|*.local|169.254/16|*.169.254/16Process finished with exit code 0

重要系统属性 (Important System Properties)

Some of the important system properties are:

一些重要的系统属性是:

“file.separator” File separator (for example, “/”)
“java.class.path” Java classpath
“java.class.version” Java class version number
“java.home” Java installation directory
“java.vendor” Java vendor-specific string
“java.vendor.url” Java vendor URL
“java.version” Java version number
“line.separator” Line separator
“os.arch” Operating system architecture
“os.name” Operating system name
“os.version” Operating system version
“path.separator” Path separator (for example, “:”)
“user.language” Language used by User
“user.dir” User’s current working directory
“user.home” User home directory
“user.name” User account name
“ file.separator” 文件分隔符(例如,“ /”)
“ java.class.path” Java类路径
“ java.class.version” Java类版本号
“ java.home” Java安装目录
“ java.vendor” Java供应商特定的字符串
“ java.vendor.url” Java供应商URL
“ java.version” Java版本号
“ line.separator” 行分隔符
“ os.arch” 操作系统架构
“ os.name” 操作系统名称
“ os.version” 作业系统版本
“ path.separator” 路径分隔符(例如,“:”)
“ user.language” 用户使用的语言
“ user.dir” 用户的当前工作目录
“ user.home” 用户主目录
“用户名” 用户帐号名称

获取特定的系统属性 (Getting a Specific System Property )

To get a particular property from the list use System.Property(key). Where key is the name of the property you want to retrieve. The output is returned as a string. If the property key doesn’t match then null is returned.

要从列表中获取特定属性,请使用System.Property(key)。 其中key是要检索的属性的名称。 输出以字符串形式返回。 如果属性键不匹配,则返回null。

public class Main {    public static void main(String[] args)    {        System.out.println(System.getProperty("java.class.path"));        System.out.println(System.getProperty("os.name"));        System.out.println(System.getProperty("user.name"));    }}
Output Properties
Output
输出量
/Users/jayant/Desktop/java/JD1/out/production/JD1Mac OS Xjayant

The three properties have been printed out.

这三个属性已被打印出来。

There is another variation that lets you specify what has to be printed in case the property name doesn’t match. Observe the difference in the fourth and fifth line in the following :

还有另一种变体,可以让您指定在属性名称不匹配的情况下必须打印的内容。 请注意以下内容在第四行和第五行中的区别:

public class Main {    public static void main(String[] args)    {System.out.println(System.getProperty("java.class.path"));System.out.println(System.getProperty("os.name"));System.out.println(System.getProperty("user.name"));System.out.println(System.getProperty("hello"));System.out.println(System.getProperty("hello","property not found"));    }}
printing properties
Output
输出量
/Users/jayant/Desktop/java/JD1/out/production/JD1Mac OS Xjayantnullproperty not found

The fourth line returns null, since “hello” doesn’t match with any property name. The fifth line returns the line we mentioned in the code “property not found”.

第四行返回null,因为“ hello”与任何属性名称都不匹配。 第五行返回我们在代码“找不到属性”中提到的行。

结论 (Conclusion )

We can retrieve system properties using the above-mentioned methods. Information such as the version of Java in use, home directory, name of Java vendor can be retrieved from the System properties.

我们可以使用上述方法检索系统属性。 可以从“系统”属性中检索诸如正在使用的Java版本,主目录,Java供应商名称之类的信息。

翻译自:

转载地址:http://bmlzd.baihongyu.com/

你可能感兴趣的文章
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>
我的Android进阶之旅------>Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>
【转】how can i build fast
查看>>
null?对象?异常?到底应该如何返回错误信息
查看>>
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
图论知识,博客
查看>>
[原创]一篇无关技术的小日记(仅作暂存)
查看>>
20145303刘俊谦 Exp7 网络欺诈技术防范
查看>>
原生和jQuery的ajax用法
查看>>
iOS开发播放文本
查看>>
20145202马超《java》实验5
查看>>
JQuery 事件
查看>>
main(argc,argv[])
查看>>