import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
public class PropertyUtil {
private static Properties properties = new Properties();
private PropertyUtil() {
}
/**
* 加载Properties文件
* @param filePath Properties文件相对路径
*/
public static void load(String filepath) {
FileInputStream fis = null;
try {
URL url = PropertyUtil.class.getResource(filepath);
if(url==null){
throw new FileNotFoundException("无法加载配置文件:" + filepath + ",请检查文件位置是否正确。");
}
File file = new File(url.getPath().replaceAll("%20", " "));
fis = new FileInputStream(file);
properties.load(fis);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 根据key获取value
* @param key
* @return
*/
public static String getString(String key) {
return properties.getProperty(key);
}
/**
* 根据key获取value,并转为int
* @param key
* @return
*/
public static int getInt(String key) {
return Integer.valueOf(properties.getProperty(key));
}
}
Properties读取文件
最新推荐文章于 2024-07-24 17:27:32 发布