java读取属性资料的方法

java读取属性文件的方法

主要有两种:

一是使用Class的getResourceAsStream方法:

private static Properties props;
	private static final String DatabaseFilePath = "/database.properties";
	
	static{
		props = new Properties();
		InputStream in = PropertiesUtil.class.getResourceAsStream(DatabaseFilePath); 
		try {
			props.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

二是使用ResourceBundle类的getBundle方法:

public static ResourceBundle CONFIG = ResourceBundle.getBundle("config");

 

三是在Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream方法:

InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);