logo

getproperty() i getproperties() metode System Class u Javi

Klasa System u Javi ima dvije metode koje se koriste za čitanje svojstava sustava: 

    getProperty: Klasa System ima dvije različite verzije getPropertyja. Oba dohvaćaju vrijednost svojstva navedenog u popisu argumenata. Jednostavnija od dvije metode getProperty uzima jedan argument.getProperties:Metoda java.lang.System.getProperties() određuje trenutna svojstva sustava.


Opis metoda:  

    getProperty(ključ niza):  Metoda java.lang.System.getProperty(String key)  vraća niz koji sadrži vrijednost svojstva. Ako svojstvo ne postoji, ova verzija getProperty vraća null. 
    Ovo se temelji na paru ključ-vrijednost kao što je navedeno u donjoj tablici.  
    Sintaksa: 
     
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.
    Implementacija: 
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  } } 
    Izlaz: 
user.dir: /tmp/hsperfdata_bot home: null os.name: Linux version: 1.8.0_101-b13 name: null
    getProperty(String key Definicija niza) :java.lang.System.getProperty(String key Definicija niza) omogućuje postavljanje definicije argumenta, tj. može se postaviti zadana vrijednost za određeni ključ. 
    Sintaksa: 
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.
    Implementacija: 
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'));  } } 
    Izlaz: 
Hello key property : Geeks System key property :For Geeks Property key property :null
    getProperties() : java.lang.System.getProperties()dohvaća trenutna svojstva koja JVM na vašem sustavu dobiva od vašeg operativnog sustava. Trenutna svojstva sustava vraćaju se kao objekt svojstava za korištenje metodom getProperties(). Ako takav skup svojstava nije prisutan, skup sustava se prvo kreira, a zatim inicijalizira. 
    Također se može modificirati postojeći skup svojstava sustava korištenjem metode System.setProperties(). Postoji broj par ključ-vrijednost u datoteci svojstava neki od njih su sljedeći: 
     
  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
    Sintaksa: 
public static Properties getProperties()   Parameters :   ------   Returns :   System properties that JVM gets on your System gets from OS
    Implementacija: 
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);  } } 
  • Izlaz: Kliknite ovdje vidjeti izlaz 
     


Važne točke:   



    java.lang.System.getProperty(ključ niza) :dohvaća samo ona svojstva - vrijednosti koje ćete navesti koristeći ključ (pridružen toj određenoj vrijednosti koju želite).java.lang.System.getProperty(String key Definicija niza) :pomaže vam da stvorite vlastite skupove ključeva i vrijednosti koje želite.java.lang.System.getProperties() :dohvaća sva svojstva - vrijednosti koje JVM na vašem sustavu dobiva od operativnog sustava.


Napravi kviz