需要学习视频资料请加qq 1686763368
在我们开发的过程中,有些属性可能不会经常修改,但是有时需要修改的时候有得从代码中一层一层的去找,很似麻烦。因此需要配置一个属性文件.properties,进行读取和方便我们来进行修改。
public class PropertiesUtil {
private static final String properiesName = "conf/config.properties";
private static Properties prop = new Properties();
static {
InputStream is = null;
try {
is = PropertiesUtil.class.getClassLoader().getResourceAsStream(properiesName);
prop.load(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String getPropery(String key) {
return prop.getProperty(key);
}
public void writeProperty(String key, String value) {
InputStream is = null;
OutputStream os = null;
Properties p = new Properties();
try {
is = new FileInputStream(properiesName);
p.load(is);
os = new FileOutputStream(PropertiesUtil.class.getClassLoader().getResource(properiesName).getFile());
p.setProperty(key, value);
p.store(os, key);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
if (null != os)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String getLogUrl(){
String path=PropertiesUtil.class.getClassLoader().getResource("").getPath();
return path.substring(0,path.indexOf("QCMS"));
}
public static void main(String[] args) {
System.out.println(PropertiesUtil.getPropery("user.name"));
}
}