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的使用及读取资源文件的更多相关文章

  1. 关于java.util.Properties读取中文乱码的正确解决方案(不要再用native2ascii.exe了)

    从Spring框架流行后,几乎根本不用自己写解析配置文件的代码了,但近日一个基础项目(实在是太基础,不能用硕大繁琐的Spring), 碰到了用java.util.Properties读取中文内容(UT ...

  2. Java.util.properties读取配置文件分析

    Java.util.properties API链接: https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html Clas ...

  3. java使用java.util.Properties读取properties文件的九种方法

    直接上代码: package com.test.test; import java.io.BufferedInputStream; import java.io.FileInputStream; im ...

  4. java.util.Properties 读取配置文件中的参数

    用法 getProperty方法的返回值是String类型. java.util.Properties 读取配置文件中的参数 //读取配置文件 FileInputStream inStream = n ...

  5. WEB应用中的普通Java程序如何读取资源文件

    package cn.itcast; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Serv ...

  6. 使用java.util.Properties类读写配置文件

    J2SE 1.5 以前的版本要求直接使用 XML 解析器来装载配置文件并存储设置,虽说也并非难事,相比 java.util.Properties却要做额外的解析工作.而java.util.Proper ...

  7. java中读取资源文件的方法

    展开全部 1.使用java.util.Properties类的load()方法 示例: //文件在项目下.不是在包下!! InputStream in = new BufferedInputStrea ...

  8. Android中使用java.util.Properties犯的错

    今天尝试使用java.util.Properties来保存应用配置,然而遇到了好几个问题,对于熟悉此内容的来说可能都是猪一样的错误,但难免有像我一样的新手再次遇到,希望此文能有所帮助. 错误1 jav ...

  9. Java/JavaWeb中读取资源文件

    1.一般工程中使用I/O类指定文件的绝对路径读取 FileInputStream fis = new FileInputStream("src/main/resources/zsm.prop ...

随机推荐

  1. NeDB——node嵌入式数据库

    参考资料1:[http://www.alloyteam.com/2016/03/node-embedded-database-nedb/] 参考资料2:[https://github.com/loui ...

  2. 成功秀了一波scala spark ML逻辑斯蒂回归

    1.直接上官方代码,调整过的,方可使用 package com.test import org.apache.spark.{SparkConf, SparkContext} import org.ap ...

  3. 关于linux中的目录配置标准以及文件基本信息

    关于Linux中的目录配置标准 在查看docker.k8的运行日志,修改相关的运行记录的时候,学长总是能很快地找到目录,这个多多少少和Linux的FHS(File Hierarchy Standard ...

  4. Python_ONLINE_习题集_02 函数封装

    2.1 封装函数实现如下要求 例如:输入2,5 则求:2 + 22+222 + 2222+22222的和 参考答案: https://www.bilibili.com/read/cv4185619 d ...

  5. JS 数组的常用方法详解归纳之改变原数组方法

    shift() 把数组的第一个元素从其中删除,并返回第一个元素的值, 如果数组是空的,那么 shift() 方法将不进行任何操作,返回 undefined 值.请注意,该方法不创建新数组,而是直接修改 ...

  6. MySQL-快速入门(6)连接查询、子查询、正则表达式查询、数据的插入删除更新

    1.内连接查询:inner join ... on 只有满足条件的记录才能够出现在结果关系中,即完全相等.自连接查询是一种特殊的内连接查询. 2.外连接查询: 1>左外连接 / 左连接:返回包括 ...

  7. Tomcat 一台机器运行多个Tomcat

    转 https://www.cnblogs.com/andy1234/p/8866588.html 在一台Win10 PC 上面同时开启两个Tomcat系统为例. 1. 硬件环境 2. 到Tomcat ...

  8. ajax异步加载分页评论带点赞功能

    <script type="text/javascript" src="__ROOT__/Index/Tpl/Public/js/jquery.js"&g ...

  9. 4-2如何判断字符串a是否以字符串b开头或结尾

    1.相关介绍 1.1修改文件权限和查看文件权限 在windows平台实验时 os.chmod()无法将文件权限修改为可执行,暂不深究如何实现.在linux平台进行测试. (1)创建三个文件 pytho ...

  10. 使用`html-webpack-plugin`插件配置启动页面

    由于使用`--contentBase`指令的过程比较繁琐,需要指定启动的目录,同时还需要修改index.html中script标签的src属性,所以推荐大家使用`html-webpack-plugin ...