logo

metódy getproperty() a getproperties() triedy System v jazyku Java

Trieda System v jazyku Java má dve metódy používané na čítanie systémových vlastností: 

    getProperty: Trieda System má dve rôzne verzie getProperty. Obaja získajú hodnotu vlastnosti pomenovanej v zozname argumentov. Jednoduchšia z dvoch metód getProperty vyžaduje jeden argument.getProperties:Metóda java.lang.System.getProperties() určuje aktuálne vlastnosti systému.


Popis metód:  

    getProperty (kľúč reťazca):  Metóda java.lang.System.getProperty(String key)  vracia reťazec obsahujúci hodnotu vlastnosti. Ak vlastnosť neexistuje, táto verzia getProperty vráti hodnotu null. 
    Toto je založené na páre kľúč – hodnota, ako je uvedené v tabuľke nižšie.  
    Syntax: 
     
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.
    Implementácia: 
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  } } 
    výstup: 
user.dir: /tmp/hsperfdata_bot home: null os.name: Linux version: 1.8.0_101-b13 name: null
    getProperty (definícia reťazca kľúča reťazca) :java.lang.System.getProperty (String key Definícia reťazca) umožňuje nastaviť definíciu argumentu, t.j. je možné nastaviť predvolenú hodnotu pre konkrétny kľúč. 
    Syntax: 
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.
    Implementácia: 
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'));  } } 
    výstup: 
Hello key property : Geeks System key property :For Geeks Property key property :null
    getProperties() : java.lang.System.getProperties()načíta aktuálne vlastnosti, ktoré JVM vo vašom systéme získava z vášho operačného systému. Aktuálne vlastnosti systému sa vrátia ako objekt Properties na použitie metódou getProperties(). Ak takáto množina vlastností neexistuje, najprv sa vytvorí a potom inicializuje množina systému. 
    Existujúcu množinu systémových vlastností je možné upraviť aj pomocou metódy System.setProperties(). Existuje množstvo pár kľúč-hodnota v súbore vlastností niektoré z nich sú nasledovné: 
     
  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
    Syntax: 
public static Properties getProperties()   Parameters :   ------   Returns :   System properties that JVM gets on your System gets from OS
    Implementácia: 
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);  } } 
  • Výstup: Kliknite tu aby ste videli výstup 
     


Dôležité body:   



    java.lang.System.getProperty (kľúč reťazca) :načíta iba tie vlastnosti - hodnoty, ktoré určíte pomocou kľúča (priradeného k určitej hodnote, ktorú chcete).java.lang.System.getProperty(definícia reťazca kľúča reťazca) :vám pomôže vytvoriť si vlastné množiny kľúč – hodnota, ktoré chcete.java.lang.System.getProperties() :načíta všetky vlastnosti – hodnoty, ktoré JVM vo vašom systéme získa z operačného systému.


Vytvoriť kvíz