Java.util.Propertiesでプロパティファイルを読み込む

1039 ワード

Java.util.Propertiesでプロパティファイルを読み込む
プロパティファイル:properties.properties
username=\u9F99\u94A4

 
読み込み:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Prop {

	public static void main(String[] s) {
		Prop prop = new Prop();
		try {
			prop.getProp();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void getProp() throws IOException {
		//  Class   getResourceAsStream()  ,              
		InputStream is = this.getClass().getClassLoader().getResourceAsStream(
				"properties.properties");
		Properties p = new Properties();
		//  Properties  load()  ,           ( /  )
		p.load(is);
		//  Properties  getProperty()  ,     value
		String username = p.getProperty("username");
		System.out.println("username=" + username);
	}
}