Java 读取、获取配置文件.properties中的数据

所属分类: 软件编程 / java 阅读数: 36
收藏 0 赞 0 分享

java获取配置文件.properties中的数据,具体内容如下所示:

方法太多,只写一种比较简单的。

 文件test1.properties内容

test1 = 123;
test2=3211
    Properties prop = new Properties();
    prop.load(new FileInputStream("src/test1.properties"));
    System.out.println(prop.get("test1"));

输出

123;1

简单封装一下,完整代码

package propertis.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class Test {
  /**
   * @param args
   * @throws IOException 
   * @throws FileNotFoundException 
   */
  public static void main(String[] args) throws FileNotFoundException, IOException {
    // TODO Auto-generated method stub
    Properties prop = new Properties();
    prop.load(new FileInputStream("src/test1.properties"));
    System.out.println(prop.get("test1"));
    System.out.println(ProUtil.getTest1Value("test1"));
    System.out.println(ProUtil.getTest1Value("test2"));
  }
}
class ProUtil{
  private static Properties prop = new Properties();
  static{
    try {
      prop.load(new FileInputStream("src/test1.properties"));
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  public static Object getTest1Value(String key){
    return prop.get(key);
  }
}

输出

123;
123;
321

下面看下Java 读取Properties配置文件

方法:

Properties properties = new Properties();
FileInputStream in = new FileInputStream("**.properties");
properties.load(in);
in.close();

配置文件:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
username=root
password=

代码实现:

import java.io.FileInputStream;
import java.util.Properties;
public class PropertiesTest {
 private static final String PROPERTIES_NAME = "db.properties";
 public static String DB_DRIVER = null;
 public static String DB_URL = null;
 public static String DB_USER = null;
 public static String DB_PWD = null;
 
 static{
 FileInputStream in = null;
 try{
  Properties properties = new Properties();
  in = new FileInputStream(PROPERTIES_NAME);
  properties.load(in);
  DB_DRIVER = properties.getProperty("driver");
  DB_URL = properties.getProperty("url");
  DB_USER = properties.getProperty("username");
  DB_PWD = properties.getProperty("passworld");
  System.out.println("读取配置信息成功!");
  showConfig();
 }catch(Exception e){
  e.printStackTrace();
  System.out.println("读取配置信息失败!");
 }finally{
  if(in != null){
  try{
   in.close();
  }catch(Exception e){
   e.printStackTrace();
  }
  }
 }
 }
 
 private static void showConfig(){
 System.out.println("-----------------------配置信息-----------------");
 System.out.println("dirver: "+DB_DRIVER);
 System.out.println("url: "+DB_URL);
 System.out.println("user: "+DB_USER);
 System.out.println("passworld: "+DB_PWD);
 System.out.println("----------------------------------------------");
 }
 
 public static void main(String[] args){
 
 }
}

运行结果:

读取配置信息成功!

-----------------------配置信息-----------------
dirver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
user: root
passworld: null
----------------------------------------------

以上所述是小编给大家介绍的Java 读取、获取配置文件.properties中的数据,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

java实现背单词程序

这篇文章主要为大家详细介绍了java实现背单词程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java实现单词查询小程序

这篇文章主要为大家详细介绍了java实现单词查询小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java程序开发环境配置图文教程

这篇文章主要为大家详细介绍了Java程序开发环境配置图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解ssh框架原理及流程

在本文中小编给大家整理的是关于ssh框架原理及流程的相关知识点内容,有此需要的朋友们可以学习下。
收藏 0 赞 0 分享

Java实现弹窗效果的基本操作

这篇文章主要为大家详细介绍了Java实现弹窗效果的基本操作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解springmvc常用5种注解

在本篇里我们给大家总结了关于springmvc常用5种注解相关知识点以及实例代码,需要的朋友们参考下。
收藏 0 赞 0 分享

Java实现弹窗效果的基本操作(2)

这篇文章主要为大家详细介绍了Java实现弹窗效果的基本操作第二篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Spring Boot假死诊断实战记录

这篇文章主要给大家介绍了关于Spring Boot假死诊断的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring Boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Java计时新姿势StopWatch详解

这篇文章主要介绍了Java计时新姿势StopWatch,最近公司来了个大佬,从他那里学到不少东西,其中一个就是计时的新姿势「StopWatch」,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现点击按钮弹出新窗体功能

这篇文章主要为大家详细介绍了java实现点击按钮弹出新窗体功能,旧窗体不进行操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多