logo

Java 시스템 클래스의 getproperty() 및 getproperties() 메소드

Java의 System 클래스에는 시스템 속성을 읽는 데 사용되는 두 가지 메서드가 있습니다. 

    get속성: System 클래스에는 두 가지 버전의 getProperty가 있습니다. 둘 다 인수 목록에 명명된 속성 값을 검색합니다. 두 가지 getProperty 메소드 중 더 간단한 메소드는 단일 인수를 사용합니다.get속성: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(String 키 문자열 정의):원하는 고유한 키-값 세트를 만드는 데 도움이 됩니다.java.lang.System.getProperties() :모든 속성(시스템의 JVM이 운영 체제에서 가져오는 값)을 가져옵니다.


퀴즈 만들기