java读取配置文件
来源:互联网 发布:ubuntu打开软件更新器 编辑:程序博客网 时间:2024/05/16 18:37
读取config.properties配置文件里传入参数为keyName的值
PropertiesReader类的代码如下
import java.io.*;
import java.util.*;public class PropertiesReader {
public static String getProperties(String keyName) {
String keyValue = null;
//System.out.println(keyName);
try {
InputStream in = PropertiesReader.class.getClassLoader().getResourceAsStream("config.properties");//读取配置文件对象
Properties format = new Properties();//读取文件的格式对象.
format.load(in);//将文件的读取对象和文件的读取格式对象,相关联.
keyValue = format.getProperty(keyName);
//System.out.println(keyValue);
} catch (Exception e) {
e.printStackTrace();
}
return keyValue;
}
}
配置文件config.properties内容如下
db.driver=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
db.uname=scott
db.uword=orcl
主函数Test如下
public class Test {
public static void main(String[] args) {
String driver = PropertiesReader.getProperties("db.driver");
String url = PropertiesReader.getProperties("db.url");
String uname = PropertiesReader.getProperties("db.uname");
String uword = PropertiesReader.getProperties("db.uword");
System.out.println(driver);
System.out.println(url);
System.out.println(uname);
System.out.println(uword);
}
}
结果输出如下
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@127.0.0.1:1521:orcl
scott
orcl
- java读取配置文件
- java读取配置文件
- java 读取properties配置文件
- Java读取properties配置文件
- JAVA读取配置文件路径
- java读取xml配置文件
- JAVA读取配置文件
- Java读取配置文件
- Java配置文件读取
- java读取xml配置文件
- java读取properties配置文件
- java 读取配置文件properties
- java读取配置文件
- Java读取配置文件
- java读取配置文件
- java读取配置文件property
- java 读取*.properties配置文件
- JAVA [ 配置文件的读取 ]
- 【Java】延迟队列DelayQueue里的坑 concurrent.Delayed
- vue文件在sublime编辑器中显示高亮
- Ubuntu16.04-修改DNS
- linux使用update-alternatives切换java版本
- 关于getContext()、getApplication()、getApplicationContext()、getActivity()的区别
- java读取配置文件
- openmp常用指令集(Fortran版)(1)
- Maven依赖中的scope详解
- GNSS钟跳探测
- jekyll bootstrap更改主题theme
- 机械振动
- PL/SQL之八 存储过程
- hdu-2066-一个人的旅行(Dijkstra)
- 关于python中带下划线的变量和函数 的意义