您的位置:首页 > 运维架构

项目运行过程中修改.properties文件问题

2015-04-07 08:47 141 查看
项目运行过程中需要修改配置文件的代码。菜鸟贴出记录自己成长。。

/**

showOu 要修改为的值   

*/

public void setAttribute(String value) {

        OutputStream out = null;

        InputStream in = null;

        if(showOu == null){

            showOu = "ou";

        }

        try {

            //获取服务器上配置文件所在位置

            File file = new File(com.avicit.tools.GetSystemPath.getClassesPath() + "show.properties");  

            if (!file.exists())  

                file.createNewFile();

            

            in = new FileInputStream(file);

           

            

            Properties p = new Properties();

            p.load(in);

            in.close();

            p.setProperty("Attr_name", value);
         //out的位置很重要。一定要在setProperties之后。不然会把原来的所有其他属性覆盖掉

           out = new FileOutputStream(file);

          //将修改后的文件存回去

            p.store(out, null);

        }

        catch (FileNotFoundException e) {

            e.printStackTrace();

        }

        catch (IOException e) {

            e.printStackTrace();

        }

        finally{

            try {

                if(out != null){

                    out.close();

                }

                if(in != null){

                    in.close();

                }

            }

            catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

注意每次修改后的配置文件在本地是不会变的,因为路径获取的是服务器上的路径,修改的也为服务器上的配置文件。

如果在项目运行过程中修改了配置文件,则再次读取配置文件之前要重新从服务器上读取一次,因为会有配置文件的缓存,不再次读取的话还是修改以前的值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java
相关文章推荐