「Java入門」getResourceAsStreamでpropertyファイルを読み込む

2022年6月26日

構文
1.getResourceAsStream(String name)
指定されたリソースを読み込む入力ストリームを返します。
InputStream is = クラス名.class.getResourceAsStream(“/propertiesファイルパス名");

2.load(InputStream inStream)
入力バイト・ストリームからキーと要素が対になったプロパティ・リストを読み込みます。

1.Common.properties

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#パスワードの有効期間
password.date=120
#一括登録maxファイル数
fileupload.count=5
#パスワードの有効期間 password.date=120 #一括登録maxファイル数 fileupload.count=5
#パスワードの有効期間
password.date=120

#一括登録maxファイル数
fileupload.count=5

2.使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesProg {
private static final Properties props;
static {
props = new Properties();
InputStream is = PropertiesProg.class.getResourceAsStream("/Common.properties");
try {
props.load(is);
is.close();
System.out.println("static method called");
System.out.println("パスワードの有効期間:" + props.getProperty("password.date")+"日");
System.out.println("一括登録maxファイル数:" + props.getProperty("fileupload.count"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("main method called ");
}
}
package com.arkgame.study; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiesProg { private static final Properties props; static { props = new Properties(); InputStream is = PropertiesProg.class.getResourceAsStream("/Common.properties"); try { props.load(is); is.close(); System.out.println("static method called"); System.out.println("パスワードの有効期間:" + props.getProperty("password.date")+"日"); System.out.println("一括登録maxファイル数:" + props.getProperty("fileupload.count")); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { System.out.println("main method called "); } }
package com.arkgame.study;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesProg {
    private static final Properties props;

    static {
        props = new Properties();
        InputStream is = PropertiesProg.class.getResourceAsStream("/Common.properties");
        try {
            props.load(is);
            is.close();
            System.out.println("static method called");
            System.out.println("パスワードの有効期間:" + props.getProperty("password.date")+"日");
            System.out.println("一括登録maxファイル数:" + props.getProperty("fileupload.count"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {

        System.out.println("main method called ");

    }

}

3.実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
static method called
パスワードの有効期間:120
一括登録maxファイル数:5
main method called
static method called パスワードの有効期間:120日 一括登録maxファイル数:5 main method called
static method called
パスワードの有効期間:120日
一括登録maxファイル数:5
main method called

 

Java

Posted by arkgame