java使用properties文件

使用对用的util包下的properties包就可以了,这样我们有配置的话,写到一个properties文件中更直观。

这里写一个比较丑的例子:

package com.property;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class MainProperty {
    public static void main(String[] args) throws IOException {
        Properties properties=new Properties();
        //路径
        String url=System.getProperty("user.dir");
        System.out.println(url);
        File dire=new File("");
        String URI=dire.getCanonicalPath();
        System.out.println(URI);
        //包名
        String packString=(new MainProperty()).getClass().getPackage().getName();
        System.out.println(packString);
        FileInputStream fis=new FileInputStream(url+"\\src\\main\\java\\com\\property\\my.properties");
        properties.load(fis);
        fis.close();
        System.out.println("sitename:"+properties.getProperty("sitename"));
        System.out.println("siteURL:"+properties.getProperty("siteURL"));
    }
}

确实比较丑,里面没有自动获取到当前的配置路径。这里可以自己写一个,比如类似spring的获取当前路径下的配置文件ClassPathXmlApplicationContext的,等用的时候再写吧。还缺少了一个properties文件,里面写几个自己想要的键值对就好了,然后打印自己的内容。

posted @ 2014-08-28 17:16  薛定谔的猫_  阅读(283)  评论(0编辑  收藏  举报