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 ...
随机推荐
- 【SQL系列】深入浅出数据仓库中SQL性能优化之Hive篇
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[SQL系列]深入浅出数据仓库中SQL性能优化之 ...
- cocos2dx基础篇(12) 编辑框之一CCTextFieldTTF
前面我们讲了精灵贴图.标签.菜单.按钮.感觉似乎少了点什么?UI控件里是不是应该还有一个很重要的控件--编辑框.在手机网游中,启动游戏,过了开场动画后,基本上显示的第一个界面应该就是游戏的登录界面了吧 ...
- POJ 1330 Nearest Common Ancestors (dfs+ST在线算法)
详细讲解见:https://blog.csdn.net/liangzhaoyang1/article/details/52549822 zz:https://www.cnblogs.com/kuang ...
- ActiveMQ学习教程/2.简单示例
ActiveMQ学习教程(二)——简单示例 一.应用IDEA构建Maven项目 File->New->Module...->Maven->勾选->选择->Next ...
- python+selenium操作cookie
WebDriver提供了操作Cookie的相关方法,可以读取.添加和删除cookie信息. WebDriver操作cookie的方法: get_cookies(): 获得所有cookie信息. get ...
- linux 文件属性文件权限
权限 -rw-------. root root Mar : anaconda-ks.cfg drwxr-xr-x root root May : dir1 drwxr-xr-x root root ...
- [转帖]功耗降50%,性能升35%!三星3nm GAA 2021年量产
功耗降50%,性能升35%!三星3nm GAA 2021年量产 http://www.chinaflashmarket.com/Instructor 在三星晶圆代工技术论坛(Samsung Found ...
- 剑指Offer编程题(Java实现)——链表中环的入口结点
题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 思路一 迭代遍历链表,利用HashSet将每个结点添加到哈希表中,如果添加失败(重复遍历了这个结点即遇到环),输出 ...
- [ARC101E]Ribbons on Tree(容斥,dp)
Description 给定一棵有 \(n\) 个节点的树,满足 \(n\) 为偶数.初始时,每条边都为白色. 现在请你将这些点两两配对成 \(\frac{n}{2}\) 个无序点对.每个点对之间的的 ...
- OI那些事——AFO
\(OI\)那些事--\(AFO\) 世界上从此少了一个\(Oier\)也不会有人知道,也许只有某个人在某年某月某日翻到了Eternal 风度的博客才会发现:哇,这哥们怎么\(Noip\)就退役了 两 ...