logo

Métodos getproperty() e getproperties() da classe de sistema em Java

A classe System em Java possui dois métodos usados ​​para ler as propriedades do sistema: 

    obterPropriedade: A classe System possui duas versões diferentes de getProperty. Ambos recuperam o valor da propriedade nomeada na lista de argumentos. O mais simples dos dois métodos getProperty utiliza um único argumento.obter propriedades:O método java.lang.System.getProperties() determina as propriedades atuais do sistema.


Descrição dos métodos:  

    getProperty(chave de string):  O método java.lang.System.getProperty(String key)  retorna uma string contendo o valor da propriedade. Se a propriedade não existir, esta versão de getProperty retornará nulo. 
    Isso se baseia no par chave-valor conforme mencionado na tabela abaixo.  
    Sintaxe: 
     
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.
    Implementação: 
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  } } 
    Saída : 
user.dir: /tmp/hsperfdata_bot home: null os.name: Linux version: 1.8.0_101-b13 name: null
    getProperty (definição de string de chave de string):java.lang.System.getProperty(String key String definition) permite definir a definição do argumento, ou seja, pode-se definir um valor padrão para uma chave específica. 
    Sintaxe: 
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.
    Implementação: 
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'));  } } 
    Saída : 
Hello key property : Geeks System key property :For Geeks Property key property :null
    getProperties() : java.lang.System.getProperties()busca as propriedades atuais que a JVM em seu sistema obtém de seu sistema operacional. As propriedades atuais do sistema são retornadas como objeto Properties para uso pelo método getProperties(). Se nenhum conjunto de propriedades estiver presente, um conjunto de sistema é primeiro criado e depois inicializado. 
    Também é possível modificar o conjunto existente de propriedades do sistema usando o método System.setProperties(). Existem vários par chave-valor no arquivo de propriedades alguns dos quais são os seguintes: 
     
  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
    Sintaxe: 
public static Properties getProperties()   Parameters :   ------   Returns :   System properties that JVM gets on your System gets from OS
    Implementação: 
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);  } } 
  • Saída: Clique aqui para ver a saída 
     


Pontos importantes:   



    java.lang.System.getProperty (chave de string):busca apenas as propriedades - valores que você especificará usando a chave (associada ao valor específico que você deseja).java.lang.System.getProperty (definição de string de chave de string):ajuda você a criar seus próprios conjuntos de valores-chave desejados.java.lang.System.getProperties() :busca todas as propriedades - valores que a JVM do seu sistema obtém do sistema operacional.


Criar questionário