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 ...
随机推荐
- 【监控实践】【4.1】利用trace实现阻塞跟踪和慢查询跟踪
原文:https://blog.csdn.net/kk185800961/article/details/49252037 分享个SQLServer profiler 的一个技巧吧.很早用过,忘记总结 ...
- Centos7 搭建pptp服务器
1.检查是否支持pptp 返回ok即表示支持 modprobe ppp-compress-18 && echo ok 2.安装ppp yum install -y ppp 3.安装pp ...
- ls 命令通配符(3)
使用过正则的人应该很熟悉通配符.如果非要解释的话,我找来一段: 通配符是一种特殊语句,主要有星号(*)和问号(?),用来模糊搜索文件. 当查找文件夹时,可以使用它来代替一个或多个真正字符:当不知道真正 ...
- 区间gcd
http://codeforces.com/problemset/problem/914/D 题意:给你n个数,两种操作:1.询问区间[l,r]在至多一次修改一个数的条件下区间gcd是否等于x. 2. ...
- JS封装插件:实现文件读写功能
scripting.FileSystemObject是一个可以实现文件读写的COM组件,由于COM组件可以被跨语言调用,因此可以选择像vbs或者JS这种脚本语言调用,下面我就使用该COM组件封装了一个 ...
- CentOS卸载lamp环境的步骤
学习PHP的时候需要在CentOS系统下安装lamp环境,安装容易卸载就没那么简单了,因为lamp由Apache.MySQL.PHP三个部分构成,需要逐个卸载,小编就给大家介绍下CentOS卸载lam ...
- python字符串替换的2种方法
python 字符串替换可以用2种方法实现:1是用字符串本身的方法.2用正则来替换字符串 下面用个例子来实验下:a = 'hello word'把a字符串里的word替换为python 1.用字符串本 ...
- python学习笔记(2):科学计算及数据可视化入门
一.NumPy 1.NumPy:Numberical Python 2.高性能科学计算和数据分析的基础包 3.ndarray,多维数组(矩阵),具有矢量运算的能力,快速.节省空间 (1)ndarray ...
- 我国三大常用坐标系:北京54、西安80和WGS-84
转自:http://blog.sina.com.cn/s/blog_6dbe2d780100mwr5.html 我国三大常用坐标系:北京54.西安80和WGS-84 1.北京54坐标系(BJZ54)北 ...
- Servlet&Http&Request笔记
# 今日内容: 1. Servlet 2. HTTP协议 3. Request ## Servlet: 1. 概念 2. 步骤 3. 执行原理 ...