logo

metody getproperty() i getproperties() klasy systemowej w Javie

Klasa System w Javie ma dwie metody używane do odczytywania właściwości systemu: 

    pobierzWłaściwość: Klasa System ma dwie różne wersje getProperty. Obydwa pobierają wartość właściwości wymienionej na liście argumentów. Prostsza z dwóch metod getProperty przyjmuje pojedynczy argument.pobierz Właściwości:Metoda java.lang.System.getProperties() określa bieżące właściwości systemu.


Opis metod:  

    getProperty(Klucz ciągu):  Metoda java.lang.System.getProperty(String key)  zwraca ciąg znaków zawierający wartość właściwości. Jeśli właściwość nie istnieje, ta wersja getProperty zwraca wartość null. 
    Opiera się to na parze klucz-wartość, jak wspomniano w tabeli podanej poniżej.  
    Składnia: 
     
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.
    Realizacja: 
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  } } 
    Wyjście : 
user.dir: /tmp/hsperfdata_bot home: null os.name: Linux version: 1.8.0_101-b13 name: null
    getProperty(Definicja ciągu znaków klucza string):java.lang.System.getProperty(String key String definicja) pozwala na ustawienie definicji argumentu, czyli można ustawić domyślną wartość dla konkretnego klucza. 
    Składnia: 
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.
    Realizacja: 
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'));  } } 
    Wyjście : 
Hello key property : Geeks System key property :For Geeks Property key property :null
    getProperties(): java.lang.System.getProperties()pobiera bieżące właściwości, które JVM w twoim systemie pobiera z twojego systemu operacyjnego. Bieżące właściwości systemu są zwracane jako obiekt Properties do wykorzystania przez metodę getProperties(). Jeśli nie ma takiego zestawu właściwości, najpierw tworzony jest zestaw systemu, a następnie inicjowany. 
    Można także modyfikować istniejący zestaw właściwości systemu za pomocą metody System.setProperties(). Istnieje wiele parę klucz-wartość w pliku właściwości niektóre z nich są następujące: 
     
  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
    Składnia: 
public static Properties getProperties()   Parameters :   ------   Returns :   System properties that JVM gets on your System gets from OS
    Realizacja: 
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);  } } 
  • Dane wyjściowe: kliknij Tutaj aby zobaczyć wynik 
     


Ważne punkty:   



    java.lang.System.getProperty(Klucz ciągu):pobiera tylko te właściwości - wartości, które określisz za pomocą klucza (powiązanego z konkretną wartością, którą chcesz).java.lang.System.getProperty(Klucz ciągu Definicja ciągu):pomaga w tworzeniu własnych zestawów klucz-wartość, które chcesz.java.lang.System.getProperties() :pobiera wszystkie właściwości - wartości, które JVM w twoim systemie pobiera z systemu operacyjnego.


Utwórz quiz