Java System类getProperties()方法及示例

系统类getProperties()方法 (System class getProperties() method)

Syntax:

句法:

    public static Properties getProperties();
    public static String getProperty(String property_name);
    public static String getProperty(String property_name, String default_value);

  • getProperties() method is available in java.lang package.

    getProperties()方法在java.lang包中可用。

  • getProperties() method is used to get the current system properties.

    getProperties()方法用于获取当前系统属性。

  • getProperty(String property_name) method is used to get the system property based on the specified property name (provided in the parameter).

    getProperty(String property_name)方法用于基于指定的属性名称(在参数中提供)获取系统属性。

  • public static String getProperty(String property_name, String default_value) method is used to get the system property based on the specified property name (provided in the parameter), and it may return the default property if the specified property does not exist.

    public static String getProperty(String property_name,String default_value)方法用于基于指定的属性名称(参数中提供)获取系统属性,如果指定的属性不存在,则它可能返回默认属性。

  • There may following exceptions occur:

    可能发生以下异常:

    • SecurityException: In this exception checkPropertyAccess() method can’t allow access to the given system property when security manager exists.SecurityException :在此异常中,当存在安全管理器时,checkPropertyAccess()方法不允许访问给定的系统属性。
    • NullPointerException: In this exception, if the given system property is null or we can say that the given system property holds null value.NullPointerException :在此异常中,如果给定的系统属性为null,或者我们可以说给定的系统属性持有null值。
    • IllegalArgumentException: In this exception, if the given system property is empty or we can say that the given system property does not hold any value.IllegalArgumentException :在此异常中,如果给定的系统属性为空,或者可以说给定的系统属性不包含任何值。

Parameter(s):

参数:

  • In the first case, there is no need to provide any parameter.

    在第一种情况下 ,不需要提供任何参数。

  • In the second case, property_name – specifies the name of the property to be returned.

    在第二种情况下 , property_name –指定要返回的属性的名称。

  • In the third case, property_name – specifies the name of the property to be returned and default_value – specifies the value to be returned, if the given property does not exist.

    在第三种情况下 , property_name –指定要返回的属性的名称, default_value –指定要返回的值(如果给定的属性不存在)。

Return value:

返回值:

  • In the first case, the return type is Properties – it returns the system properties.

    在第一种情况下 ,返回类型为Properties-它返回系统属性。

  • In the second case, the return type is String – it returns the specified property as a string.

    在第二种情况下 ,返回类型为String –它以字符串形式返回指定的属性。

  • In the third case, the return type is String – it returns the specified property (or default system property) as a string.

    在第三种情况下 ,返回类型为String –它以字符串形式返回指定的属性(或默认系统属性)。

Java程序演示getProperties()方法的示例 (Java program to demonstrate the example of getProperties() method)

// Java program to demonstrate the example of 
// getProperties() method of System Class

import java.lang.*;
import java.util.Properties;

public class GetPropertiesMethod {
    public static void main(String[] args) {
        System.out.println("Example of getProperties()...");
        System.out.println("Display JVM information");
        // Property Object 
        Properties property = System.getProperties();
        System.out.println(property);
        System.out.println();

        System.out.println("Example of getProperties(property_name)...");
        // Printing directory 
        System.out.println("java.vm.name: " + System.getProperty("java.vm.name"));
        // Printing library path
        System.out.println("java.library.path: " + System.getProperty("java.library.path"));
        // Printing name of operating system
        System.out.println("os.name: " + System.getProperty("os.name"));
        // Printing version of operating system
        System.out.println("os.version: " + System.getProperty("os.version"));
        System.out.println();

        System.out.println("Example of getProperties(property_name, default_value)...");
        System.out.println("os.version: " + System.getProperty("os.version", "It's not a property"));
        System.out.println("os.java: " + System.getProperty("os.java", "It's not a property"));
    }
}

Output

输出量

E:\Programs>javac GetPropertiesMethod.java
E:\Programs>java GetPropertiesMethod
Example of getProperties()...
Display JVM information
{awt.toolkit=sun.awt.X11.XToolkit, java.specification.version=10, file.encoding.pkg=sun.io, 
sun.cpu.isalist=, sun.jnu.encoding=ANSI_X3.4-1968, java.class.path=*:., 
java.vm.vendor="Oracle Corporation", sun.arch.data.model=64, 
java.vendor.url=http://java.oracle.com/, user.timezone=, os.name=Linux, 
java.vm.specification.version=10, sun.java.launcher=SUN_STANDARD, user.country=US, 
sun.boot.library.path=/usr/lib/jvm/java-10-jdk/lib, sun.java.command=GetPropertiesMethod, 
jdk.debug=release, sun.cpu.endian=little, user.home=/root, user.language=en, 
java.specification.vendor=Oracle Corporation, java.version.date=2018-04-17, 
java.home=/usr/lib/jvm/java-10-jdk, file.separator=/, 
java.vm.compressedOopsMode=32-bit, line.separator=, 
java.specification.name=Java Platform API Specification, 
java.vm.specification.vendor=Oracle Corporation, 
java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment, 
sun.management.compiler=HotSpot 64-Bit Tiered Compilers, java.runtime.version=10.0.1+10, 
user.name=root, path.separator=:, os.version=4.8.0-41-generic, 
java.runtime.name=Java(TM) SE Runtime Environment, file.encoding=ANSI_X3.4-1968, 
java.vm.name=Java HotSpot(TM) 64-Bit Server VM, java.vendor.version=18.3, 
java.vendor.url.bug=http://bugreport.java.com/bugreport/, java.io.tmpdir=/tmp, 
java.version=10.0.1, user.dir=/home, os.arch=amd64, 
java.vm.specification.name=Java Virtual Machine Specification, 
java.awt.printerjob=sun.print.PSPrinterJob, sun.os.patch.level=unknown, 
java.library.path=/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib, 
java.vendor=Oracle Corporation, java.vm.info=mixed mode, java.vm.version=10.0.1+10, 
sun.io.unicode.encoding=UnicodeLittle, java.class.version=54.0}

Example of getProperties(property_name)...
java.vm.name: Java HotSpot(TM) 64-Bit Server VM
java.library.path: /usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib
os.name: Linux
os.version: 4.8.0-41-generic

Example of getProperties(property_name, default_value)...
os.version: 4.8.0-41-generic
os.java: It's not a property


翻译自: https://www.includehelp.com/java/system-class-getproperties()-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值