logo

getproperty() и getproperties() методи на системния клас в Java

Класът System в Java има два метода, използвани за четене на системни свойства: 

    getProperty: Класът System има две различни версии на getProperty. И двете извличат стойността на свойството, посочено в списъка с аргументи. По-простият от двата метода getProperty приема един аргумент.getProperties:Методът java.lang.System.getProperties() определя текущите свойства на системата.


Описание на методите:  

    getProperty(ключ за низ):  Методът java.lang.System.getProperty(String key)  връща низ, съдържащ стойността на свойството. Ако свойството не съществува, тази версия на getProperty връща null. 
    Това се основава на двойка ключ - стойност, както е посочено в таблицата по-долу.  
    Синтаксис: 
     
public static String getProperty(String key)   Parameters :   key : key whose system property we want   Returns :   System property as specified the key Null : 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_bot home: null os.name: Linux version: 1.8.0_101-b13 name: null
    getProperty(String ключ Дефиниция на низ):java.lang.System.getProperty(String key String definition) позволява да се зададе дефиницията на аргумента, т.е. може да се зададе стойност по подразбиране за конкретен ключ. 
    Синтаксис: 
public static String getProperty(String key String def)   Parameters :   key : system property def : default value of the key to be specified   Returns :   System Property Null : 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 : Geeks System key property :For Geeks Property 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);  } } 
  • Изход: Щракнете тук за да видите резултата 
     


Важни точки:   



    java.lang.System.getProperty(ключ за низ):извлича само тези свойства - стойности, които ще посочите с помощта на ключа (свързан с тази конкретна стойност, която искате).java.lang.System.getProperty(Ключ на низ Дефиниция на низ):ви помага да създадете ваши собствени набори ключ-стойности, които искате.java.lang.System.getProperties() :извлича всички свойства - стойности, които JVM на вашата система получава от операционната система.


Създаване на тест