JAVA操作属性文件
来源:互联网 发布:培训班软件 编辑:程序博客网 时间:2024/05/16 11:36
-
-
-
-
-
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.util.Properties;
- import java.util.ResourceBundle;
-
-
-
-
-
- publicclass OperatePropertiesFile {
- public OperatePropertiesFile() {
- }
-
-
-
-
-
-
- private String getValueByPropertyName(String propertiesFileName,String propertyName) {
- String s="";
- Properties p=new Properties();
- FileInputStream in;
- try {
-
- in = new FileInputStream(propertiesFileName);
- p.load(in);
- in.close();
- s=p.getProperty(propertyName);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return s;
- }
-
-
-
-
-
-
- private String getValueByPropertyName_(String propertiesFileNameWithoutPostfix,String propertyName) {
- String s="";
-
- ResourceBundle bundel = ResourceBundle.getBundle(propertiesFileNameWithoutPostfix);
- s=bundel.getString(propertyName);
- return s;
- }
-
-
-
-
-
-
-
- privateboolean changeValueByPropertyName(String propertiesFileName,String propertyName,String propertyValue) {
- boolean writeOK=true;
- Properties p=new Properties();
- FileInputStream in;
- try {
- in = new FileInputStream(propertiesFileName);
- p.load(in);
- in.close();
- p.setProperty(propertyName,propertyValue);
-
- FileOutputStream out=new FileOutputStream(propertiesFileName);
- p.store(out,"Just Test");
- out.flush();
- out.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return writeOK;
- }
- publicstaticvoid main(String[] args) {
- OperatePropertiesFile operatePropertiesFile = new OperatePropertiesFile();
- operatePropertiesFile.changeValueByPropertyName("db.properties","DBLocation","D://Palfinger//palfinger.mdb");
- }
- } 假如有一个属性文件db.properties如下:
DBLocation=D/://Palfinger//palfinger.mdb