JAVA开发10--加载相对路径下配置文件并读取内容

描述:

java开发,使用 FileInputStream 加载 文件,读取文件内容。


在项目文件夹的同目录下,有一个文件夹 config,此文件夹下有一个配置文件 var2.properties;

文件内容如下:

YEAR:2016
MONTH:07
DATA_TYPE:OA_ProjectBudget


代码如下:

package com.noahark.dao;

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

public class Test {

    public static void main(String[] args) throws IOException {

        //加载配置文件
        FileInputStream fis = new FileInputStream(System.getProperty("user.dir")+"\\config\\var2.properties");
        Properties properties = new Properties();
        if(fis !=null)
        properties.load(fis);

        // 获取信息
        String year_value = properties.getProperty("YEAR");
        String month_value = properties.getProperty("MONTH");
        String data_type = properties.getProperty("DATA_TYPE");
        int year = Integer.parseInt(year_value);
        int month = Integer.parseInt(month_value);        
        System.out.println("year: " + year + "\nmonth: " + month + "\ndata_type: " + data_type);


    }
}


输出结果如下:

year: 2016
month: 7
data_type: OA_ProjectBudget



posted @ 2016-10-20 17:15  Stepheng  阅读(74)  评论(0)    收藏  举报