Java中的System类有两种用于读取系统属性的方法:
null
- getProperty: System类有两个不同版本的getProperty。两者都检索参数列表中命名的属性的值。两个getProperty方法中较简单的一个只接受一个参数。
- getProperties: 爪哇。lang.System。getProperties()方法确定当前系统属性。
方法说明:
- getProperty(字符串键): JAVAlang.System。getProperty(字符串键)方法返回一个包含属性值的字符串。如果属性不存在,此版本的getProperty将返回null。 这是基于下表中提到的键值对。 语法:
public static String getProperty(String key)Parameters :key : key whose system property we wantReturns :System property as specified the keyNull : if there is no property present with that key.
- 实施:
JAVA
// Java Program illustrating the working of getProperty(String key) method import java.lang.*; import java.util.Properties; public class NewClass { public static void main(String[] args) { // Printing Name of the system property System.out.println( "user.dir: " +System.getProperty( "user.dir" )); // Fetches the property set with 'home' key System.out.println( "home: " +System.getProperty( "home" )); // Resulting in Null as no property is present // Printing 'name of Operating System' System.out.println( "os.name: " +System.getProperty( "os.name" )); // Printing 'JAVA Runtime version' System.out.println( "version: " +System.getProperty( "java.runtime.version" )); // Printing 'name' property System.out.println( "name: " +System.getProperty( "name" )); // Resulting in Null as no property is present } } |
- 输出:
user.dir: /tmp/hsperfdata_bothome: nullos.name: Linuxversion: 1.8.0_101-b13name: null
- getProperty(字符串键、字符串定义): JAVAlang.System。getProperty(字符串键、字符串定义)允许设置参数定义,即可以为特定键设置默认值。 语法:
public static String getProperty(String key, String def)Parameters :key : system property def : default value of the key to be specified Returns :System PropertyNull : if there is no property present with that key.
- 实施:
JAVA
// Java Program illustrating the working of // getProperty(String key, String definition) method import java.lang.*; import java.util.Properties; public class NewClass { public static void main(String[] args) { // use of getProperty(String key, String definition) method // Here key = "Hello" and System Property = "Geeks" System.out.println( "Hello property : " + System.getProperty( "Hello" , "Geeks" )); // Here key = "Geek" and System Property = "For Geeks" System.out.println( "System-property :" + System.getProperty( "System" , "For Geeks" )); // Here key = "Property" and System Property = null System.out.println( "Property-property :" + System.getProperty( "Property" )); } } |
- 输出:
Hello key property : GeeksSystem key property :For GeeksProperty key property :null
- getProperties():java。lang.System。getProperties() 获取系统上的JVM从操作系统获取的当前属性。当前系统属性作为properties对象返回,供getProperties()方法使用。如果不存在这样的属性集,则首先创建一组系统,然后初始化。 还可以使用system来修改现有的系统属性集。setProperties()方法。有很多 属性文件中的键值对 ,其中一些如下:
Keys Values--> os.version : OS Version --> os.name : OS Name--> os.arch : OS Architecture --> java.compiler : Name of the compiler you are using--> java.ext.dirs : Extension directory path--> java.library.path : Paths to search libraries whenever loading--> path.separator : Path separator--> file.separator : File separator--> user.dir : Current working directory of User--> user.name : Account name of User--> java.vm.version : JVM implementation version--> java.vm.name : JVM implementation name--> java.home : Java installation directory--> java.runtime.version : JVM version
- 语法:
public static Properties getProperties()Parameters :------Returns :System properties that JVM gets on your System gets from OS
- 实施:
JAVA
// Java Program illustrating the working of getProperties() method import java.lang.*; import java.util.Properties; public class NewClass { public static void main(String[] args) { /* Use of getProperties() method System class refers to the JVM on which you are compiling your JAVA code getProperty fetches the actual properties that JVM on your System gets from your Operating System */ System.out.println( "Following are the JVM information of your OS :" ); System.out.println( "" ); // Property Object Properties jvm = System.getProperties(); jvm.list(System.out); } } |
- 输出:点击 在这里 查看输出
要点:
- JAVAlang.System。getProperty(字符串键): 只获取那些属性——您将使用键指定的值(与您想要的特定值关联)。
- JAVAlang.System。getProperty(字符串键、字符串定义): 帮助您创建自己想要的键值集。
- JAVAlang.System。getProperties(): 获取所有属性——系统上的JVM从操作系统获取的值。
本文由 莫希特·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END