logo

mètodes getproperty() i getproperties() de la classe del sistema a Java

La classe System a Java té dos mètodes utilitzats per llegir les propietats del sistema: 

    getProperty: La classe System té dues versions diferents de getProperty. Tots dos recuperen el valor de la propietat anomenada a la llista d'arguments. El més senzill dels dos mètodes getProperty pren un sol argument.getProperties:El mètode java.lang.System.getProperties() determina les propietats actuals del sistema.


Descripció dels mètodes:  

    getProperty (clau de cadena):  El mètode java.lang.System.getProperty(String key)  retorna una cadena que conté el valor de la propietat. Si la propietat no existeix, aquesta versió de getProperty retorna null. 
    Això es basa en el parell clau-valor tal com s'esmenta a la taula que es mostra a continuació.  
    Sintaxi: 
     
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.
    Implementació: 
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  } } 
    Sortida: 
user.dir: /tmp/hsperfdata_bot home: null os.name: Linux version: 1.8.0_101-b13 name: null
    getProperty(String key Definició de cadena):java.lang.System.getProperty(String key String definition) permet establir la definició de l'argument, és a dir, es pot establir un valor predeterminat per a una clau específica. 
    Sintaxi: 
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.
    Implementació: 
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'));  } } 
    Sortida: 
Hello key property : Geeks System key property :For Geeks Property key property :null
    getProperties(): java.lang.System.getProperties()recupera les propietats actuals que la JVM del vostre sistema obté del vostre sistema operatiu. Les propietats actuals del sistema es tornen com a objecte Properties per utilitzar-les amb el mètode getProperties(). Si no hi ha aquest conjunt de propietats, primer es crea un conjunt de sistema i després s'inicializa. 
    També es pot modificar el conjunt existent de propietats del sistema mitjançant el mètode System.setProperties(). Hi ha un nombre de parell clau-valor al fitxer de propietats alguns dels quals són els següents: 
     
  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
    Sintaxi: 
public static Properties getProperties()   Parameters :   ------   Returns :   System properties that JVM gets on your System gets from OS
    Implementació: 
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);  } } 
  • Sortida: Feu clic aquí per veure la sortida 
     


Punts importants:   



    java.lang.System.getProperty (clau de cadena):només obté aquelles propietats: valors que especificareu amb la clau (associada a aquest valor en particular que voleu).java.lang.System.getProperty(Definició de cadena de claus de cadena):us ajuda a crear els vostres propis conjunts de valors-clau que vulgueu.java.lang.System.getProperties():recupera totes les propietats: valors que la JVM del vostre sistema obté del sistema operatiu.


Crea un qüestionari