プロパティファイルの読み込み

1746 ワード


public class Mytest 
public static void readFile(String fileName) {// fileName (file.properties)
		InputStream in = null;
		Properties pros = new Properties();
		try {
			if (null != fileName) {
// Mytest 
		in = Mytest.class.getResourceAsStream(fileName);
// , 
				pros.load(in);
Enumeration en = pros.propertyNames();// key 
		while (en.hasMoreElements()) {
	String key = (String) en.nextElement();
System.out.println("key=" + key + " value=" + pros.getProperty(key));
// key value 
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println(" ");
		} finally {
			try {
			if (null != in) {
				in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println(" ");
			}
		}

	}

2つの方法:

 logger.info("==== ===="
				+ AddTempLotteryUser.class.getResource("/").getPath());

		InputStream in2 = new FileInputStream(AddTempLotteryUser.class
				.getResource("/").getPath()
				+ "UserInterfaceCenter.properties");

		logger.info(" :" + in2.available());

		Properties p = new Properties();
		try {
			InputStream in1 = AddTempLotteryUser.class
					.getResourceAsStream("/UserInterfaceCenter.properties");
			System.out.println("count==" + in1.available());
			p.load(in1);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}