加载Properties静态文件

一、spring加载配置文件

<bean id="propertyConfigurer"  

       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  

    <property name="location">  

        <value>classpath:ldap.properties</value>  

    </property>  

</bean>  

<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">  

    <property name="url" value="${url}" />  

    <property name="base" value="${base}" />  

    <property name="userDn" value="${userDn}" />  

    <property name="password" value="${password}" />  

</bean> 

二、java加载静态文件:

1. 在包中新建一个类 Config.java



package Project;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;



public class Config {


    public static String CONNECTION_TYPE;
    public static String CONNECTION_URL;
    public static String CONNECTION_USER;
    public static String CONNECTION_PWD;
    public static String CONNECTION_DRIVER;
    public static String FILE_STORE_PATH;
    public static String FTP_IP;
    public static String FTP_USERNAME;
    public static String FTP_PASSWORD;
    public static String FTP_PORT;
    public static String FTP_CHOICE_STORE;
    public static String FTP_CHOICE_AUTO;
    private static Properties prop = new Properties();


    static {
        try {
            prop.load(Config.class.getResourceAsStream("DatabaseProperties.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //get the data from the .prop file
        CONNECTION_TYPE = prop.getProperty("conn_type");
        CONNECTION_URL = prop.getProperty("conn_url");
        CONNECTION_USER = prop.getProperty("conn_userName");
        CONNECTION_PWD = prop.getProperty("conn_passWord");
        CONNECTION_DRIVER = prop.getProperty("conn_driver");
        FILE_STORE_PATH = prop.getProperty("file_store_path");
        FTP_IP = prop.getProperty("ftp_ip");
        FTP_USERNAME = prop.getProperty("ftp_userName");
        FTP_PASSWORD = prop.getProperty("ftp_passWord");
        FTP_PORT = prop.getProperty("ftp_port");
        FTP_CHOICE_STORE = prop.getProperty("ftp_choice_store");
        FTP_CHOICE_AUTO = prop.getProperty("ftp_choice_auto");
       
    }


    //write the data to .prop file
    public static void setProperty(String filePath, String parameterKey, String parameterValue){
        Properties prop = new Properties();
        try{
            InputStream in = new FileInputStream(filePath);
            prop.load(in);
            FileOutputStream ou = new FileOutputStream(filePath);
            prop.setProperty(parameterKey, parameterValue);
            prop.store(ou, filePath);
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}


2. 新建一个 DatabaseProperties.properties 文件,内容如下:


 


conn_driver=test_1


ftp_choice_auto=test_2
ftp_ip=test_3


ftp_passWord=test_4
conn_userName=test_5
ftp_choice_store=test_6
file_store_path=test_7
conn_type=test_8
conn_passWord=test_9
ftp_userName=test_10
conn_url=test_11


ftp_port=test_12


 


3. 写一个测试的例子:Test.java


 


package Project;


public class Test {
    public static void main(String args[]){


        // get the data from .prop file
        System.out.println(Config.CONNECTION_DRIVER);
        String s1="new test";


        // set the new data for .prop file
        Config.setProperty("src\\Project\\DatabaseProperties.properties", "conn_driver", s1);


        // show the newest data from .prop file
        System.out.println(Config.CONNECTION_DRIVER);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值