Java读取配置文件属性
package com.chenwl;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class AppProperties {
private static Properties prop = null;
static {
prop = new Properties();
try {
//1种指定文件的位置
//InputStream inputStream = new FileInputStream("E:\\workspace\\java\\src\\config.properties");
//2种得到classload来加载配置文件,不用在关心文件"config.properties"的具体位置
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("config.properties");
prop.load(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getProperties(String key){
String value =prop.getProperty(key);
if(value==null){
value = "";
}
return value.trim();
}
public static void main(String[] args) {
String ip = AppProperties.getProperties("ip");
System.out.println(ip);
}
}
- 51.la

招生简章


