修改或读取配置文件

//注意,properties文件只会在编译后的classes文件夹里有一个对应的文件,不会改变src下的文件



  String abc = "config_wsSys.properties";
Properties pro = new Properties();
//this.getClass().getClassLoader().getResourceAsStream(this.getClass().getClassLoader().getResource(abc).getPath());
/* this.getClass().getClassLoader().getResourceAsStream()首先从java虚拟机中查找文件
,如果找不到,再从类路径查找,找到后放到虚拟机中
,也就是说第一次读取的时候已经把文件放到Java虚拟机中了,所以再读取的时候,只要服务器不关,直接从虚拟机中读取,
而每次更新的是类路径下的文件,所以每次读取不到更新的数据
* */

/*

File file = new File(this.getClass().getClassLoader().getResource(path).getPath());
InputStream fis = new FileInputStream(file);//读取配置文件流
pro.load(fis);
String a = pro.getProperty("app.sys.isopen.transfer");
String b = pro.getProperty("app.sys.isopen.gametogoldmoney");
*/

File file = new File(this.getClass().getClassLoader().getResource(abc).getPath());
InputStream fis = new FileInputStream(file);//读取配置文件流
pro.load(fis);

/*读取配置文件
* String a = pro.getProperty("cfg_sys.transfer");
String b = pro.getProperty("cfg_sys.gameGold");*/

FileOutputStream fos = new FileOutputStream(new File(this.getClass().getClassLoader().getResource(abc).getPath()));
pro.setProperty("cfg_sys.gameGold", "1");//设置配置文件
pro.setProperty("cfg_sys.transfer", "1");
pro.store(fos, null);
fis.close();
fos.close();  

你可能感兴趣的:(虚拟机)