How to read properties file in java

摘要属性文件在java项目中被用于外部化配置,例如,数据库设置。在这篇文章中,我们将看到如何在java中阅读属性文件。

Java使用Properties类存储在键值对之上。属性。load方法以键值对的形式加载属性文件非常方便。

属性文件看起来是这样的。


有两种方法可以做到这一点。

  • Read properties file from system
  • Read properties file  from classpath

Read properties file from System :

在这一点上,您需要从系统路径读取属性文件。这里我将属性文件放在项目的根级别。


java code:

package cn.micai.io;

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

/**
 * 描述:How to read properties file in java
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/7 13:25
 */
public class ReadPropertiesFileJavaMain {

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

        System.out.println("Reading from properties file");
        System.out.println("-----------------------------");
        Properties prop = getPropertyFromSystem("src/main/resources/config.properties");
        System.out.println("host : "+prop.getProperty("host"));
        System.out.println("username : "+prop.getProperty("username"));
        System.out.println("password : "+prop.getProperty("password"));
        System.out.println("-----------------------------");

        /*ReadPropertiesFileJavaMain readPropertiesFileJavaMain = new ReadPropertiesFileJavaMain();
        System.out.println("Reading from properties file");
        System.out.println("-----------------------------");
        Properties properties = readPropertiesFileJavaMain.readPropertiesFileFromClasspath("/config.properties");
        System.out.println("host : " + properties.getProperty("host"));
        System.out.println("username : " + properties.getProperty("username"));
        System.out.println("password : " + properties.getProperty("password"));
        System.out.println("-----------------------------");*/


    }

    // Read properties file from System
    private static Properties getPropertyFromSystem(String fileName) throws IOException {
        FileInputStream fileInputStream = null;
        Properties properties = null;
        try {
            fileInputStream = new FileInputStream(fileName);
            // create Properties class object
            properties = new Properties();
            // load properties file into it
            properties.load(fileInputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fileInputStream.close();
        }
        return properties;
    }

    // Read properties file from classpath
    public Properties readPropertiesFileFromClasspath(String fileName) throws IOException {
        InputStream inputStream = null;
        Properties properties = null;
        try {
            properties = new Properties();
            inputStream = this.getClass().getResourceAsStream(fileName);

            // create Properties class object
            if (inputStream != null) {
                // load properties file into it
                properties.load(inputStream);
            } else {
                throw new FileNotFoundException("property file '" + fileName + "' not found in the classpath");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
        return properties;
    }
}

当您运行在程序之上时,您将得到以下输出:


Read properties file from classpath: 


您也可以将属性文件读取到类路径。您有$project/src作为默认的classpath,因为这个src文件夹将被复制到类中。你可以把它放在$project/src文件夹中,然后从那里读取它。


你需要使用

this.getClass().getResourceAsStream("/config.properties");

去读取它从classpath下

package cn.micai.io;

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

/**
 * 描述:How to read properties file in java
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/7 13:25
 */
public class ReadPropertiesFileJavaMain {

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

        /*System.out.println("Reading from properties file");
        System.out.println("-----------------------------");
        Properties prop = getPropertyFromSystem("src/main/resources/config.properties");
        System.out.println("host : "+prop.getProperty("host"));
        System.out.println("username : "+prop.getProperty("username"));
        System.out.println("password : "+prop.getProperty("password"));
        System.out.println("-----------------------------");*/

        ReadPropertiesFileJavaMain readPropertiesFileJavaMain = new ReadPropertiesFileJavaMain();
        System.out.println("Reading from properties file");
        System.out.println("-----------------------------");
        Properties properties = readPropertiesFileJavaMain.readPropertiesFileFromClasspath("/config.properties");
        System.out.println("host : " + properties.getProperty("host"));
        System.out.println("username : " + properties.getProperty("username"));
        System.out.println("password : " + properties.getProperty("password"));
        System.out.println("-----------------------------");


    }

    // Read properties file from System
    private static Properties getPropertyFromSystem(String fileName) throws IOException {
        FileInputStream fileInputStream = null;
        Properties properties = null;
        try {
            fileInputStream = new FileInputStream(fileName);
            // create Properties class object
            properties = new Properties();
            // load properties file into it
            properties.load(fileInputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fileInputStream.close();
        }
        return properties;
    }

    // Read properties file from classpath
    public Properties readPropertiesFileFromClasspath(String fileName) throws IOException {
        InputStream inputStream = null;
        Properties properties = null;
        try {
            properties = new Properties();
            inputStream = this.getClass().getResourceAsStream(fileName);

            // create Properties class object
            if (inputStream != null) {
                // load properties file into it
                properties.load(inputStream);
            } else {
                throw new FileNotFoundException("property file '" + fileName + "' not found in the classpath");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
        return properties;
    }
}

当您运行在程序之上时,您将得到以下输出:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迷彩的博客

你的鼓励将是我最大的创作动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值