properties配置文件的运用可以给我们提供很多的方便,比如说我们可以把需要灵活配置的 路径 或者其它变量放在其中,以后需要修改的时候就只要修改配置文件即可。


不需要一个个地方的去修改 代码。


比如我现在有一个property.properties配置文件放在src路径下,也可以放到WEB-INF下。


如图

java获取properties配置文件_类名


properties中我存放了路径变量

java获取properties配置文件_类名_02


获取的方式如下:

DownloadSamplesFile.class 

public static void main(String[] args) {
private static Properties properties = new Properties();
	InputStream is = DownloadSamplesFile.class.getClassLoader().getResourceAsStream("property.properties");  
        try {
			properties.load(is);
		} catch (IOException e1) {			
			e1.printStackTrace();
		}  
        String filepath = properties.getProperty("FILEPATH"); 
}



其中的DownloadSamplesFile与类名一致,配置文件名,还有参数名与自己的命名一致