在运行时在属性文件中设置值 [英] setting value inside property file at run time

查看:66
本文介绍了在运行时在属性文件中设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在运行时设置属性文件中的值.

is it possible to set the value inside property file at runtime.

我试过了,它在读回属性时发生了变化,但是当我签入文件时,更改没有反映出来.

I tried this and it changes while reading property back, but when I check in file that changes are not reflected.

       try {
            Properties props = new Properties();
            props.setProperty("ServerAddress", "ee");
            props.setProperty("ServerPort", "123");
            props.setProperty("ThreadCount", "456");
            File f = new File("myApp.properties");

            OutputStream out = new FileOutputStream( f );
            props.store(out, "This is an optional header comment string");

            System.out.println(props.get("ServerPort"));
            out.close();
       }
        catch (Exception e ) {
            e.printStackTrace();
        }

输出:123.

我需要在整个应用程序范围内拥有一个属性文件,并且可以通过 GUI 网页更改配置.

my need is to have one property file application wide and through GUI web page that configuration can be change.

推荐答案

我认为部分问题在于每次运行该代码时都会覆盖当前存在的文件.您需要做的是将当前存在的属性加载到 Properties 对象中,然后更改需要更改的属性的值,最后存储这些属性.像这样:

I think part of the problem is that you are overwriting the currently existing file every time you run that code. What you need to do is load the properties that currently exist into the Properties object, and then change the values of the properties that need changing, and finally store those properties. Something like this:

OutputStream out = null;
        try {

            Properties props = new Properties();

            File f = new File("myApp.properties");
            if(f.exists()){

                props.load(new FileReader(f));
                //Change your values here
                props.setProperty("ServerAddress", "ThatNewCoolValue");
            }
            else{

                //Set default values?
                props.setProperty("ServerAddress", "DullDefault");
                props.setProperty("ServerPort", "8080");
                props.setProperty("ThreadCount", "456");

                f.createNewFile();
            }



            out = new FileOutputStream( f );
            props.store(out, "This is an optional header comment string");

            System.out.println(props.get("ServerPort"));

       }
        catch (Exception e ) {
            e.printStackTrace();
        }
        finally{

            if(out != null){

                try {

                    out.close();
                } 
                catch (IOException ex) {

                    System.out.println("IOException: Could not close myApp.properties output stream; " + ex.getMessage());
                    ex.printStackTrace();
                }
            }
        }

第一次运行后的结果:

  #This is an optional header comment string
  #Thu Feb 28 13:04:11 CST 2013
  ServerPort=8080
  ServerAddress=DullDefault
  ThreadCount=456

第二次运行(以及每次后续运行)后的结果:

Results after second run (and each subsequent run):

  #This is an optional header comment string
  #Thu Feb 28 13:04:49 CST 2013
  ServerPort=8080
  ServerAddress=ThatNewCoolValue
  ThreadCount=456

这篇关于在运行时在属性文件中设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆