解析.properties文件
来源:互联网 发布:怎么做数据透视表 编辑:程序博客网 时间:2024/06/03 09:01
.properties文件一定要放在src目录下
要解析.properties文件,先创建一个jdbc.properties文件。
driverClass=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/day07?useSSL=trueuser=rootpassword=12345
第一种方式:使用java.util.Properties
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Properties;public class TestProperties {public static void main(String[] args) {// 1.先创建Properties实例Properties prop = new Properties();try {// 2.从jdbc.properties的文件流中加载键值对prop.load(new FileInputStream("src/jdbc.properties"));// 3.获取键所对应的值String driverClass = prop.getProperty("driverClass");String url = prop.getProperty("url");String user = prop.getProperty("user");String password = prop.getProperty("password");System.out.println(driverClass);System.out.println(url);System.out.println(user);System.out.println(password);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
输出结果为:
driverClass=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/day07?useSSL=trueuser=rootpassword=12345
第二种方式:ResourceBundle
import java.util.ResourceBundle;public class TestResourceBundle {public static void main(String[] args) {// 1.根据jdbc.properties文件的文件名(不含后缀)创建一个ResourceBundle实例ResourceBundle bundle = ResourceBundle.getBundle("jdbc");// 2.根据键获取值String driverClass = bundle.getString("driverClass");String url = bundle.getString("url");String user = bundle.getString("user");String password = bundle.getString("password");System.out.println(driverClass);System.out.println(url);System.out.println(user);System.out.println(password);}}
输出结果同上。
阅读全文
0 0
- spring解析Properties文件
- 解析properties文件内容
- 解析*.properties文件
- 如何解析properties文件
- Properties文件解析
- java解析properties文件
- Spring解析properties文件
- java解析properties文件
- RCP解析properties文件
- ResouyrceBunle 解析properties文件
- java 解析properties 文件
- Java Properties文件解析
- Java解析Properties文件
- java解析properties文件
- 解析properties文件
- java解析Properties文件
- Python:解析properties文件
- properties文件的解析
- 22 简体形
- Vue2.0史上最全入坑教程(上)—— 搭建Vue脚手架(vue-cli)
- C/C++语言为什么要有宏?
- Python序列化——pickle模块
- spring boot完美集成dwr3.0.2-RELEASE
- 解析.properties文件
- ArcGIS Server 站点架构-Web Adaptor
- Eclipse Console 加大显示的行数,禁止弹出
- Nodo登陆注册表单,数据库md5加密
- 51Nod 1024 矩阵中不重复的元素
- spring-data-redis 使用pipeline批量设置过期时间的bug
- ubuntu下环境变量详解 bashrc, profile, environment
- ionic3 实现懒加载方式 ---学习记录
- leetcode 327. Count of Range Sum