java 读取项目中的和磁盘上的properties文件

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.ResourceBundle; /** * * <b>Application name:</b><br> * <b>Application describing:</b> <br> */ public class PropertiesUtil { /** * * {读取项目中的properties配置文件}<br> * 返回的是ResourceBundle的对象 * * @param propertiesPath * @return */ public static ResourceBundle readFromProject(String propertiesFilePath) { return ResourceBundle.getBundle(propertiesFilePath); } /** * * {读取磁盘上的properties配置文件}<br> * 返回的市Properties的对象 * @param propertiesFilePath * @return */ public static Properties readFromSystemFile(String propertiesFilePath) { InputStream is = null; Properties properties = null; try { is = new FileInputStream(propertiesFilePath); properties = new Properties(); properties.load(is); } catch (FileNotFoundException e) { System.out.println("没有找到文件"); e.printStackTrace(); } catch (IOException e) { System.out.println("读取properties文件失败"); e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return properties; } /** * * {主函数} * * @param args */ public static void main(String[] args) { //ResourceBundle resourceBundle = readFromProject("db1"); ResourceBundle resourceBundle = readFromProject("com.neusoft.config.db"); String username = resourceBundle.getString("username"); String password = resourceBundle.getString("password"); String url = resourceBundle.getString("url"); String driverClassName = resourceBundle.getString("driverClassName"); System.out.println(username); System.out.println(password); System.out.println(url); System.out.println(driverClassName); String filePath = "c:" + File.separator + "test" + File.separator + "config.properties"; Properties properties = readFromSystemFile(filePath); String base = properties.getProperty("base"); String step = properties.getProperty("step"); System.out.println(base + " " + step); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值