欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-25 15:21:57
java获取配置文件.properties中的数据,具体内容如下所示: 方法太多,只写一种比较简单的。  文件test1.properties内容...

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中的数据,希望对大家有所帮助