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 ...
随机推荐
- 手把手教你搭建一个 Elasticsearch 集群
为何要搭建 Elasticsearch 集群 凡事都要讲究个为什么.在搭建集群之前,我们首先先问一句,为什么我们需要搭建集群?它有什么优势呢? 高可用性 Elasticsearch 作为一个搜索引擎, ...
- Jmeter---BeanShell 常用的 vars, get, props, put ,log用法
BeanShell介 BeanShell是用Java写成的,一个小型的.免费的.可以下载的.嵌入式的Java源代码解释器,具有对象脚本语言特性.本篇只记录一下基本的使用.有以下五个组件: Beansh ...
- Django 基于角色的权限控制
有一种场景, 要求为用户赋予一个角色, 基于角色(比如后管理员,总编, 编辑), 用户拥有相应的权限(比如管理员拥有所有权限, 总编可以增删改查, 编辑只能增改, 有些页面的按钮也只有某些角色才能查看 ...
- C盘无损扩容(傻逼拯救者128G固态分两个盘)
下载DiskGenius.exe 进行拆分分区(我从d盘拆分出20G给c盘) 然后右键此电脑,管理->磁盘管理 选中刚分出来的20G空间指向到c盘
- Express中间件body-parser
在http请求种,POST.PUT.PATCH三种请求方法中包含着请求体,也就是所谓的request,在Nodejs原生的http模块中,请求体是要基于流的方式来接受和解析. body-parser是 ...
- centos 7 里如何判断IP是否合法
ip=123.23.2.32; [[ $ip =~ ^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9 ...
- 多线程15-ReaderWriterLockSlim
)); } ); rwl.EnterUpgradeableReadLock(); ...
- SVN的各种符号含义,svn的星号,感叹号,问号等含义
黄色感叹号(有冲突):--这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许你提交,防止你的提交覆盖了别人 ...
- centos7使用kubeadm搭建kubernetes集群
一.本地实验环境准备 服务器虚拟机准备 IP CPU 内存 hostname 192.168.222.129 >=2c >=2G master 192.168.222.130 >=2 ...
- Pycharm2019.1.3破解
搬运: T3ACKYHDVF-eyJsaWNlbnNlSWQiOiJUM0FDS1lIRFZGIiwibGljZW5zZWVOYW1lIjoi5bCP6bifIOeoi+W6j+WRmCIsImFzc ...