主要用到了spring框架的ClassPathResource,可以直接获取resources目录下的文件,而不用费半天劲写绝对路径了。
import org.springframework.core.io.ClassPathResource;
import java.io.*;
import java.util.Properties;
public class PropertitesUtil {
public static Properties props;
static {
try {
readPropertiesFile("/model.properties");
} catch (IOException e) {
e.printStackTrace();
}
}
public static Properties readPropertiesFile(String filePath) throws FileNotFoundException, IOException {
InputStream inputStream = null;
try {
ClassPathResource classPathResource = new ClassPathResource(filePath);
inputStream = classPathResource.getInputStream();
props = new Properties();
props.load(new InputStreamReader(inputStream, "UTF-8"));
return props;
} catch (Exception e) {
e.printStackTrace();
return null;