logo

Metodele getproperty() și getproperties() ale clasei de sistem în Java

Clasa System din Java are două metode utilizate pentru a citi proprietățile sistemului: 

    getProperty: Clasa System are două versiuni diferite de getProperty. Ambele preiau valoarea proprietății numite în lista de argumente. Cea mai simplă dintre cele două metode getProperty ia un singur argument.getProperties:Metoda java.lang.System.getProperties() determină proprietățile curente ale sistemului.


Descrierea metodelor:  

    getProperty(cheie șir):  Metoda java.lang.System.getProperty(String key)  returnează un șir care conține valoarea proprietății. Dacă proprietatea nu există, această versiune de getProperty returnează null. 
    Aceasta se bazează pe perechea cheie-valoare, așa cum este menționat în tabelul de mai jos.  
    Sintaxa: 
     
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.
    Implementare: 
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  } } 
    Ieșire: 
user.dir: /tmp/hsperfdata_bot home: null os.name: Linux version: 1.8.0_101-b13 name: null
    getProperty(String key Definiție șir):java.lang.System.getProperty(String key String definition) permite setarea definiției argumentului, adică se poate seta o valoare implicită pentru o anumită cheie. 
    Sintaxa: 
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.
    Implementare: 
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'));  } } 
    Ieșire: 
Hello key property : Geeks System key property :For Geeks Property key property :null
    getProperties(): java.lang.System.getProperties()preia proprietățile curente pe care le primește JVM de pe sistemul dvs. de la sistemul dvs. de operare. Proprietățile curente ale sistemului sunt returnate ca obiect Proprietăți pentru a fi utilizate de metoda getProperties(). Dacă nu este prezent un astfel de set de proprietăți, un set de sistem este mai întâi creat și apoi inițializat. 
    De asemenea, se poate modifica setul existent de proprietăți ale sistemului folosind metoda System.setProperties(). Există un număr de perechea cheie-valoare în fișierul de proprietăți dintre care unele sunt după cum urmează: 
     
  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
    Sintaxa: 
public static Properties getProperties()   Parameters :   ------   Returns :   System properties that JVM gets on your System gets from OS
    Implementare: 
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);  } } 
  • Ieșire: Faceți clic Aici pentru a vedea rezultatul 
     


Puncte importante:   



    java.lang.System.getProperty(cheie șir):preia numai acele proprietăți - valori pe care le veți specifica folosind cheia (asociată acelei valori particulare pe care o doriți).java.lang.System.getProperty(String key Definiția șirului):vă ajută să vă creați propriile seturi cheie-valoare dorite.java.lang.System.getProperties() :preia toate proprietățile - valorile pe care JVM-ul de pe sistemul dumneavoastră le primește de la sistemul de operare.


Creați un test