java.util.Properties的使用及读取资源文件
1、工具类Utils
package com.oy.utils; import java.io.BufferedInputStream;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties; /**
* @author oy
* @date 2019年6月9日 下午7:20:33
* @version 1.0.0
*/
public class Utils { // 根据资源文件的绝对路径获取Properties
public static Properties getPropertiesByFilePath(String path) {
Properties properties = new Properties();
InputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(path));
properties.load(bis);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(bis);
}
return properties;
} // 使用ClassLoader来获取类路径下的资源
// 资源路径path: 前面没有"/", 相对于classpath目录
public static Properties getPropertiesByClassLoader(String path) {
Properties properties = new Properties();
InputStream is = Utils.class.getClassLoader().getResourceAsStream(path);
try {
properties.load(is);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(is);
}
return properties;
} // 使用Class来获取类路径下的资源
// 资源路径path: 以"/"开头, 相对于classpath目录; 不以"/"开头, 相对当前.class文件所在目录
public static Properties getPropertiesByClass(String path) {
Properties properties = new Properties();
InputStream is = Utils.class.getResourceAsStream(path);
try {
properties.load(is);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(is);
}
return properties;
} // 使用ClassLoader来获取类路径下的资源
// 资源路径path: 前面没有"/", 相对于classpath目录
public static InputStream getInputStreamByClassLoader(String path) {
InputStream is = Utils.class.getClassLoader().getResourceAsStream(path);
return is;
} // 使用Class来获取类路径下的资源
// 资源路径path: 以"/"开头, 相对于classpath目录; 不以"/"开头, 相对当前.class文件所在目录
public static InputStream getInputStreamByClass(String path) {
InputStream is = Utils.class.getResourceAsStream(path);
return is;
} // 关闭io流对象
public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
2、在src下新建jdbc.properties
driverClass = com.mysql.jdbc.Driver
jdbcURL = jdbc:mysql://127.0.0.1:3306/db_test?useUnicode=true&characterEncoding=utf-8
username = root
password = root
name=张三
3、测试
package com.oy.test; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Map.Entry;
import java.util.Properties; import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test; import com.oy.utils.Utils; /**
* @author oy
* @date 2019年6月9日 下午7:15:49
* @version 1.0.0
*/
public class Demo01 { @Test
public void test1() {
Properties pps = System.getProperties();
pps.list(System.out);
} @Test
public void test2() {
Properties properties = Utils.getPropertiesByClassLoader("jdbc.properties");
// properties.list(System.out); // 遍历
for (Entry<Object, Object> entry : properties.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
} // 遍历
Enumeration en = properties.propertyNames();
while(en.hasMoreElements()) {
String strKey = (String) en.nextElement();
String strValue = properties.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
} // getProperty: 根据key获取value
System.out.println("username=" + properties.getProperty("username"));
System.out.println("password=" + properties.getProperty("password")); // setProperty: 根据key修改value
properties.setProperty("password", "root001"); // properties.clear(); 删除所有键值对 try {
properties.store(new FileOutputStream("d:/jdbc.txt"), "数据库连接配置");
} catch (Exception e) {
e.printStackTrace();
}
} @Test
public void test3() {
InputStream inputStream = Utils.getInputStreamByClassLoader("jdbc.properties");
String s = "";
try {
// 读取输入流,转换成字符串
s = IOUtils.toString(inputStream);
System.out.println(s); // 字符串写入到文件
IOUtils.write(s, new FileOutputStream("d:/jdbc2.txt"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果 jdbc.txt
#\u6570\u636E\u5E93\u8FDE\u63A5\u914D\u7F6E
#Sun Jun :: CST
driverClass=com.mysql.jdbc.Driver
name=\u00E5\u00BC\u00A0\u00E4\u00B8\u0089
password=root001
jdbcURL=jdbc\:mysql\://127.0.0.1\:3306/db_test?useUnicode\=true&characterEncoding\=utf-8
username=root
jdbc2.txt
driverClass = com.mysql.jdbc.Driver
jdbcURL = jdbc:mysql://127.0.0.1:3306/db_test?useUnicode=true&characterEncoding=utf-8
username = root
password = root
name=张三
4、注意
上面代码中使用了commons-io-2.5.jar
IOUtils.toString(inputStream);
IOUtils.write(s, new FileOutputStream("d:/jdbc2.txt"));
java.util.Properties的使用及读取资源文件的更多相关文章
- 关于java.util.Properties读取中文乱码的正确解决方案(不要再用native2ascii.exe了)
从Spring框架流行后,几乎根本不用自己写解析配置文件的代码了,但近日一个基础项目(实在是太基础,不能用硕大繁琐的Spring), 碰到了用java.util.Properties读取中文内容(UT ...
- Java.util.properties读取配置文件分析
Java.util.properties API链接: https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html Clas ...
- java使用java.util.Properties读取properties文件的九种方法
直接上代码: package com.test.test; import java.io.BufferedInputStream; import java.io.FileInputStream; im ...
- java.util.Properties 读取配置文件中的参数
用法 getProperty方法的返回值是String类型. java.util.Properties 读取配置文件中的参数 //读取配置文件 FileInputStream inStream = n ...
- WEB应用中的普通Java程序如何读取资源文件
package cn.itcast; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Serv ...
- 使用java.util.Properties类读写配置文件
J2SE 1.5 以前的版本要求直接使用 XML 解析器来装载配置文件并存储设置,虽说也并非难事,相比 java.util.Properties却要做额外的解析工作.而java.util.Proper ...
- java中读取资源文件的方法
展开全部 1.使用java.util.Properties类的load()方法 示例: //文件在项目下.不是在包下!! InputStream in = new BufferedInputStrea ...
- Android中使用java.util.Properties犯的错
今天尝试使用java.util.Properties来保存应用配置,然而遇到了好几个问题,对于熟悉此内容的来说可能都是猪一样的错误,但难免有像我一样的新手再次遇到,希望此文能有所帮助. 错误1 jav ...
- Java/JavaWeb中读取资源文件
1.一般工程中使用I/O类指定文件的绝对路径读取 FileInputStream fis = new FileInputStream("src/main/resources/zsm.prop ...
随机推荐
- laradock 部署 php 环境 和 laravel/lumen 框架
环境是windows 10 版本1809,docker 版本18.09.0 首先是下载docker.git, 具体可以参考 http://laradock.io/ 要求 Docker >= 17 ...
- Java之类的继承
说起来Java的类,不得不说以下几个方面:继承.转型.重写.多态和接口. 今天来说一说继承,转型和重写几个方面: 继承(extends)即子类继承父类,就好比玻璃杯.保温杯等子类继承了杯子这个父类,子 ...
- 深入理解java:2.2. 同步锁Synchronized及其实现原理
同步的基本思想 为了保证共享数据在同一时刻只被一个线程使用,我们有一种很简单的实现思想,就是 在共享数据里保存一个锁 ,当没有线程访问时,锁是空的. 当有第一个线程访问时,就 在锁里保存这个线程的标识 ...
- java中的命名规则
转载自:http://growstep.diandian.com/post/2011-08-17/3989094 1.类名首字母应该大写.属性(成员变量).方法.对象变量以及所有标识符(如形式参数.实 ...
- WCF书籍
<WCF 服务编程> <WCF 全面解析> <WCF全面解析(套装上下册)>
- 根据select的option文本选中对应的选项
function selectByOptionTxt(obj,txt){ var optionArr = $(obj).find("option"); for(var i=0;i& ...
- 我在拼多多百亿补贴时买了个MAC probook16G苹果笔记本电脑用来写代码,有图有真相,靠谱吗?
在拼多多买了拼了个MACBOOK pro,有点担心质量问题,商家承诺有电子普票,有大哥在上面买过贵重物品吗?欢迎评论留言交流
- Clover的简单使用
官网: http://cn.ejie.me 操作说明相关: 方便的 Tab 页功能 要掌握功能强大,操作简单的标签页,只需记住Ctrl+T新开页面,Ctrl+W关闭页面,Ctrl+Tab切换页面,工作 ...
- 开篇——从程序员到IT经理
2002年~2005年我在广州的广东水力电力职业技术学院求学,主修网络工程.求学期间,我从事最多的就是玩游戏,当时就是玩MU和CS,所以有一门编程课叫C语言的“肥佬”(广东话)了,要补考,没办法,于是 ...
- net 架构师-数据库-sql server-001-SQL Server中的对象
1.1 数据库的构成 1.2 数据库对象概述 1.2.1 数据库对象 RDBMS 关系数据库管理系统 对象:数据库.索引.事务日志.CLR程序集.表 .报表.文件组.全文目录.图表.用户自定义数据类型 ...