java 读取配置文件 与更新

笔记

public class Config {

     private static Properties props = new Properties();
     static File configFile = null;
        static {
            InputStreamReader reader = null;
            try {
                File dir = new File(System.getProperty("user.dir"));
                configFile = new File(dir, "config.properties.dev");
                if (!configFile.exists()) {
//                    String path = Thread.currentThread().getClass().getClassLoader().getResource("config.properties").getPath();
//                    configFile = new File(path);
                    configFile = new File(dir, "config.properties");
                }
                reader = new InputStreamReader(new FileInputStream(configFile), "UTF-8");
                props.load(reader);
            } catch (FileNotFoundException e) {
            } catch (Exception e) {
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                    }
                }
            }
        }

        public static String getDsl(){
            System.out.println("dsl" + props.getProperty("dsl"));
            return props.getProperty("dsl");
        }

        public static String getDate(){
            System.out.println("date" + props.getProperty("date"));
            return props.getProperty("date");
        }

        public static void updateProperties(String keyname,String keyvalue) {
            try {
                props.load(new FileInputStream(configFile));
                // 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。
                // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
                props.setProperty(keyname, keyvalue);
                OutputStream fos = new FileOutputStream(configFile);
                // 以适合使用 load 方法加载到 Properties 表中的格式,
                // 将此 Properties 表中的属性列表(键和元素对)写入输出流
                props.store(fos, "Update ‘" + keyname + "‘ value");
            } catch (IOException e) {
                System.err.println("属性文件更新错误");
            }
        }   

}
时间: 2024-07-31 11:13:56

java 读取配置文件 与更新的相关文章

Java读取配置文件的方式

Java读取配置文件的方式-笔记 1       取当前启动目录下的配置文件 一般来讲启动java程序的时候,在启动的目录下会有配置文件 classLoader.getResource("").getFile()  会取到java当前启动项目的目录,然后指定对应的配置文件路径即可比如conf/conf.properties //取当前启动目录的配置文件 String filePath =classLoader.getResource("").getFile()+&q

Java 读取配置文件 Properties

String filePath="src/cn/ac/iscas/pebble/ufe/conf/id.properties"; InputStream in = new BufferedInputStream(new FileInputStream(filePath)); int i = 0; Properties p = new Properties(); p.load(in); p.getProperty(key); 还有一个方式如下: public class ConfigUt

java 读取配置文件工具类 (how to read values from properties file in java)

Java 读取配置文件工具类 使用 java.util.Properties import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiesReader { private static Properties prop; static { reload(); } private static void reload() { prop = new

JAVA读取配置文件的方法

目录 JAVA读取配置文件的方法 普通java项目 WEB项目 JAVA读取配置文件的方法 普通java项目 1.classLoader //主要通过当前类的加载器加载classpath下的资源文件,局限是classpath下的 //getResourceAsStream的路径相当于${classpath}/ 参数相对于这个路径来的 Properties properties = new Properties(); InputStream in = PaySupportUtils.class.g

转:java读取配置文件的几种方法

转自: http://www.iteye.com/topic/56496 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置文件来完成,本文根据笔者工作中用到的读取配置文件的方法小小总结一下,主要叙述的是spring读取配置文件的方法. 一.读取xml配置文件 (一)新建一个java bean(HelloBean.java) java 代码 package chb.demo.vo;   public class HelloBean {   private String hell

java 读取配置文件

java.util.Properties是对properties这类配置文件的映射.支持key-value类型和xml类型两种. key-value类型的配置文件大略长这样: 复制代码 #测试环境配置:平台路径配置 jstrd_home=D:/TMS2006/webapp/tms2006/WEB-INF/ dbPort = localhost databaseName = myd dbUserName = root 复制代码 #打头的是注释行,Properties会忽略注释.允许只有key没有v

使用Java读取配置文件

实现起来,相对比较简单,留个备案吧,废话也不多说,请看代码: package com.jd.***.config; import org.junit.*; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * 用Java读取一个配置文件 * * Created by zhanghao10 on 2016/6/5. */ public class TestProperti

java读取配置文件常用的四种方式

配置文件 放置在src下面 obj.properties className=com.store.order.dao.impl.OrderDaoImpl 方式一 @Test public void test1() throws Exception{ //文件放在src下面.eclipse会自动拷贝一份到bin目录下,或者build/classes下面, InputStream is = Class.forName("com.store.test.test").getClassLoade

java读取配置文件的几种方法

一.读取xml配置文件 (一)新建一个java bean(HelloBean.java) 1 package XX.XXX.XXX; 2 3 public class HelloBean { 4 private String helloWorld; 5 6 public String getHelloWorld() { 7 return helloWorld; 8 } 9 10 public void setHelloWorld(String helloWorld) { 11 this.hell