logo

Java のシステム クラスの getproperty() メソッドと getproperties() メソッド

Java の System クラスには、システム プロパティを読み取るために使用される 2 つのメソッドがあります。 

    プロパティの取得: System クラスには、getProperty の 2 つの異なるバージョンがあります。どちらも、引数リストで指定されたプロパティの値を取得します。 2 つの getProperty メソッドのうち単純な方のメソッドは、引数を 1 つとります。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(文字列キー文字列定義) :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 がオペレーティング システムから取得する現在のプロパティを取得します。現在のシステム プロパティは、 getProperties() メソッドで使用するための Properties オブジェクトとして返されます。そのようなプロパティのセットが存在しない場合は、システムのセットが最初に作成され、次に初期化されます。 
    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 がオペレーティング システムから取得する値をフェッチします。


クイズの作成