logo

שיטות getproperty() ו-getproperties() של System Class ב-Java

למחלקת System ב-Java יש שתי שיטות המשמשות לקריאת מאפייני מערכת: 

    getProperty: למחלקה System יש שתי גרסאות שונות של getProperty. שניהם מאחזרים את הערך של המאפיין ששמו ברשימת הארגומנטים. השיטות הפשוטות מבין שתי השיטות getProperty דורשות ארגומנט יחיד.getProperties:שיטת java.lang.System.getProperties() קובעת את מאפייני המערכת הנוכחיים.


תיאור השיטות:  

    getProperty(מפתח מחרוזת):  השיטה java.lang.System.getProperty(String key)  מחזירה מחרוזת המכילה את הערך של המאפיין. אם המאפיין אינו קיים גרסה זו של getProperty מחזירה null. 
    זה מבוסס על צמד מפתח - ערך כפי שהוזכר בטבלה המופיעה להלן.  
    תחביר: 
     
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.
    יישום: 
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  } } 
    פלט: 
user.dir: /tmp/hsperfdata_bot home: null os.name: Linux version: 1.8.0_101-b13 name: null
    getProperty(String key String definition):java.lang.System.getProperty(String key String definition) מאפשר להגדיר את הגדרת הארגומנט כלומר ניתן להגדיר ערך ברירת מחדל עבור מפתח ספציפי. 
    תחביר: 
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.
    יישום: 
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'));  } } 
    פלט: 
Hello key property : Geeks System key property :For Geeks Property key property :null
    getProperties() : java.lang.System.getProperties()מביא את המאפיינים הנוכחיים ש-JVM במערכת שלך מקבל ממערכת ההפעלה שלך. מאפייני המערכת הנוכחיים מוחזרים כאובייקט Properties לשימוש בשיטת getProperties()‎. אם אין קבוצה כזו של מאפיינים נוצרת תחילה קבוצה של מערכת ולאחר מכן מאתחלת. 
    אפשר גם לשנות את הסט הקיים של מאפייני המערכת באמצעות שיטת System.setProperties() . יש מספר של צמד מפתח-ערך בקובץ המאפיינים חלקם הם כדלקמן: 
     
  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
    תחביר: 
public static Properties getProperties()   Parameters :   ------   Returns :   System properties that JVM gets on your System gets from OS
    יישום: 
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);  } } 
  • פלט: לחץ כָּאן כדי לראות את הפלט 
     


נקודות חשובות:   



    java.lang.System.getProperty(מפתח מחרוזת):מביא רק את המאפיינים האלה - ערכים שתציין באמצעות המפתח (המשויך לערך הספציפי שאתה רוצה).java.lang.System.getProperty(הגדרת מחרוזת מפתח מחרוזת):עוזר לך ליצור ערכות מפתח-ערך משלך שאתה רוצה.java.lang.System.getProperties() :מביא את כל המאפיינים - ערכים שה-JVM במערכת שלך מקבל ממערכת ההפעלה.


צור חידון