// 读取配置文件
Properties pro = new Properties();
InputStream in = null;
try {
in = AutoDeliveryController.class.getResource("/ftp.properties").openStream();
pro.load(in);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
获取文件目录方法二(linux下会找不到目录)
String path = PropertyUtil.class.getResource("/").getPath();
path = path.substring(1, path.indexOf("classes"));
in = new FileInputStream(path+"property/emlApp.properties");
获取文件目录方法三(通用)
*配置文件要在src目录下*
in = QueryJobImpl.class.getClassLoader().getResourceAsStream("/emlApp.properties");
---------------------------------------------------------------------------------------------------------------------------
// 写文件
Properties pro = new Properties();FileOutputStream oFile = null;
InputStream in = null;
try {
in = AutoDeliveryController.class.getResource("/ftp.properties").openStream();
pro.load(in);
in.close();
oFile = new FileOutputStream(AutoDeliveryController.class.getResource("/ftp.properties").getPath());
pro.setProperty("addr", globConf[0]);
pro.setProperty("ftp",globConf[1]);
pro.setProperty("path",globConf[2]);
pro.setProperty("userName",globConf[3]);
pro.setProperty("password",globConf[4]);
pro.store(oFile, "properties file");
oFile.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
oFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}