logo

Métodos getproperty() y getproperties() de System Class en Java

La clase System en Java tiene dos métodos utilizados para leer las propiedades del sistema: 

    obtener propiedad: La clase System tiene dos versiones diferentes de getProperty. Ambos recuperan el valor de la propiedad nombrada en la lista de argumentos. El más simple de los dos métodos getProperty toma un solo argumento.obtener propiedades:El método java.lang.System.getProperties() determina las propiedades actuales del sistema.


Descripción de métodos:  

    getProperty (clave de cadena):  El método java.lang.System.getProperty(String key) devuelve una cadena que contiene el valor de la propiedad. Si la propiedad no existe, esta versión de getProperty devuelve nulo. 
    Esto se basa en el par clave-valor como se menciona en la tabla que figura a continuación.  
    Sintaxis: 
     
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ón: 
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  } } 
    Producción : 
user.dir: /tmp/hsperfdata_bot home: null os.name: Linux version: 1.8.0_101-b13 name: null
    getProperty (definición de cadena de clave de cadena):java.lang.System.getProperty (definición de cadena de clave de cadena) permite establecer la definición del argumento, es decir, se puede establecer un valor predeterminado para una clave específica. 
    Sintaxis: 
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ón: 
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'));  } } 
    Producción : 
Hello key property : Geeks System key property :For Geeks Property key property :null
    getProperties(): java.lang.System.getProperties()recupera las propiedades actuales que JVM en su sistema obtiene de su sistema operativo. Las propiedades actuales del sistema se devuelven como objeto Propiedades para que las utilice el método getProperties(). Si no existe tal conjunto de propiedades, primero se crea un conjunto de sistemas y luego se inicializa. 
    También se puede modificar el conjunto existente de propiedades del sistema utilizando el método System.setProperties(). Hay numero de par clave-valor en el archivo de propiedades algunos de los cuales son los siguientes: 
     
  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
    Sintaxis: 
public static Properties getProperties()   Parameters :   ------   Returns :   System properties that JVM gets on your System gets from OS
    Implementación: 
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);  } } 
  • Salida: haga clic aquí para ver la salida 
     


Puntos importantes:   



    java.lang.System.getProperty (clave de cadena):recupera solo esas propiedades: valores que especificará utilizando la clave (asociada a ese valor particular que desea).java.lang.System.getProperty (definición de cadena de clave de cadena):le ayuda a crear sus propios conjuntos clave-valor que desee.java.lang.System.getProperties() :recupera todas las propiedades: valores que la JVM de su sistema obtiene del sistema operativo.


Crear cuestionario