logo

metodi getproperty() e getproperties() della classe System in Java

La classe System in Java ha due metodi utilizzati per leggere le proprietà del sistema: 

    getProperty: La classe System ha due diverse versioni di getProperty. Entrambi recuperano il valore della proprietà denominata nell'elenco degli argomenti. Il più semplice dei due metodi getProperty accetta un singolo argomento.getProperties:Il metodo java.lang.System.getProperties() determina le proprietà del sistema corrente.


Descrizione dei metodi:  

    getProperty(chiave String):  Il metodo java.lang.System.getProperty(String key)  restituisce una stringa contenente il valore della proprietà. Se la proprietà non esiste questa versione di getProperty restituisce null. 
    Questo si basa sulla coppia chiave-valore come indicato nella tabella riportata di seguito.  
    Sintassi: 
     
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.
    Attuazione: 
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  } } 
    Produzione : 
user.dir: /tmp/hsperfdata_bot home: null os.name: Linux version: 1.8.0_101-b13 name: null
    getProperty(Definizione stringa chiave String):java.lang.System.getProperty(String key String Definition) consente di impostare la definizione dell'argomento, ovvero è possibile impostare un valore predefinito per una chiave specifica. 
    Sintassi: 
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.
    Attuazione: 
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'));  } } 
    Produzione : 
Hello key property : Geeks System key property :For Geeks Property key property :null
    getProperties() : java.lang.System.getProperties()recupera le proprietà correnti che JVM sul tuo sistema ottiene dal tuo sistema operativo. Le proprietà System correnti vengono restituite come oggetto Properties per l'utilizzo da parte del metodo getProperties(). Se non è presente alcun insieme di proprietà, viene prima creato e quindi inizializzato un insieme di sistemi. 
    È anche possibile modificare il set esistente di proprietà di sistema utilizzando il metodo System.setProperties(). Ce ne sono numerosi coppia chiave-valore nel file delle proprietà alcuni dei quali sono i seguenti: 
     
  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
    Sintassi: 
public static Properties getProperties()   Parameters :   ------   Returns :   System properties that JVM gets on your System gets from OS
    Attuazione: 
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);  } } 
  • Uscita: fare clic Qui per vedere l'output 
     


Punti importanti:   



    java.lang.System.getProperty(chiave String):recupera solo le proprietà, i valori che specificherai utilizzando la chiave (associata a quel particolare valore che desideri).java.lang.System.getProperty(Definizione stringa chiave String):ti aiuta a creare gli insiemi di valori-chiave che desideri.java.lang.System.getProperties() :recupera tutte le proprietà, ovvero i valori che la JVM sul sistema ottiene dal sistema operativo.


Crea quiz