/*
* 功能描述:读取资源文件的一个key对应的值
* @param key 资源文件中对应的key;例如:com.cn.test
* @param file_path 资源文件的路径,可以为绝对路径;例如:/test.properties
*/
private static String readProperties(String key, String file_path) {
Properties props = new Properties();
InputStream inStream = Main.class.getResourceAsStream(file_path);
try {
props.load(inStream);
inStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return props.getProperty(key);
}
/*
* 功能描述:向资源文件中写入数据,注意资源文件一定要放到src跟目录哦
* @param key 资源文件中对应的key;例如:com.cn.test
* @param value 资源文件中key对应的value;
* @param file_name 注意:资源文件一定要放到src跟目录哦;例如:test.properties
*/
public static void saveFile(String key, String value, String file_name) {
String filePath = Main.class.getResource("/").getPath() + file_name;
File file = new File(filePath);
InputStream fis = null;
OutputStream outputFile = null;
Properties props = new Properties();
try {
fis = new FileInputStream(filePath);
// 从输入流中读取属性列表(键和元素对)
props.load(fis);
outputFile = new FileOutputStream(file);
props.setProperty(key, value);
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
props.store(outputFile, "Update '" + value + "' value");
outputFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
* 功能描述:读取资源文件的一个key对应的值
* @param key 资源文件中对应的key;例如:com.cn.test
* @param file_path 资源文件的路径,可以为绝对路径;例如:/test.properties
*/
private static String readProperties(String key, String file_path) {
Properties props = new Properties();
InputStream inStream = Main.class.getResourceAsStream(file_path);
try {
props.load(inStream);
inStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return props.getProperty(key);
}
/*
* 功能描述:向资源文件中写入数据,注意资源文件一定要放到src跟目录哦
* @param key 资源文件中对应的key;例如:com.cn.test
* @param value 资源文件中key对应的value;
* @param file_name 注意:资源文件一定要放到src跟目录哦;例如:test.properties
*/
public static void saveFile(String key, String value, String file_name) {
String filePath = Main.class.getResource("/").getPath() + file_name;
File file = new File(filePath);
InputStream fis = null;
OutputStream outputFile = null;
Properties props = new Properties();
try {
fis = new FileInputStream(filePath);
// 从输入流中读取属性列表(键和元素对)
props.load(fis);
outputFile = new FileOutputStream(file);
props.setProperty(key, value);
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
props.store(outputFile, "Update '" + value + "' value");
outputFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}