欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Properties文件读取学习笔记

发布时间:2025/5/22 编程问答 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Properties文件读取学习笔记 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

第一种

package com.letv.shop.loadtest.util;import java.io.*; import java.util.Properties;/*** @author lw* @createTime 2018/7/30 10:41* @description 配置文件读取*/ public class PropeUtil {private String filePath;private Properties prop;public PropeUtil() {}/*** 构造方法** @param filePath*/public PropeUtil(String filePath) {this.filePath = filePath;this.prop = readProperties();}/*** 读取资源文件,并处理中文乱码** @return*/private Properties readProperties() {Properties properties = new Properties();try {InputStream inputStream = PropeUtil.class.getClassLoader().getResourceAsStream(filePath);BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));properties.load(bf);inputStream.close(); // 关闭流} catch (IOException e) {e.printStackTrace();}return properties;}/*** 获取某项文本内容** @param key* @return*/public String getPro(String key) {if (prop.containsKey(key)) {return prop.getProperty(key);} else {System.out.println("获取的键不存在");return "";}}/*** 写入某项文本内容** @param key* @return*/public void setProp(String key, String value) {if (prop == null) {prop = new Properties();}try {OutputStream outputStream = new FileOutputStream(filePath);BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));prop.setProperty(key, value);prop.store(bw, key + " value");bw.close();outputStream.close();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) throws Exception {PropeUtil p = new PropeUtil("cof/fileUrl.properties");System.out.println(p.getPro("URL"));System.out.println(p.getPro("FileName_path"));System.out.println(p.getPro("jmeter_path"));System.out.println(p.getPro("execcommand"));} }


第二种:

package com.itheima.uitl;import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties;public class JDBCUtil {static String driverClass = null;static String url = null;static String name = null;static String password= null;static{try {//1. 创建一个属性配置对象Properties properties = new Properties();InputStream is = new FileInputStream("jdbc.properties");//使用类加载器,去读取src底下的资源文件。 后面在servlet // InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");//导入输入流。properties.load(is);//读取属性driverClass = properties.getProperty("driverClass");url = properties.getProperty("url");name = properties.getProperty("name");password = properties.getProperty("password");} catch (Exception e) {e.printStackTrace();}}/*** 获取连接对象* @return*/public static Connection getConn(){Connection conn = null;try {Class.forName(driverClass);//静态代码块 ---> 类加载了,就执行。 java.sql.DriverManager.registerDriver(new Driver());//DriverManager.registerDriver(new com.mysql.jdbc.Driver());//DriverManager.getConnection("jdbc:mysql://localhost/test?user=monty&password=greatsqldb");//2. 建立连接 参数一: 协议 + 访问的数据库 , 参数二: 用户名 , 参数三: 密码。conn = DriverManager.getConnection(url, name, password);} catch (Exception e) {e.printStackTrace();}return conn;}/*** 释放资源* @param conn* @param st* @param rs*/public static void release(Connection conn , Statement st , ResultSet rs){closeRs(rs);closeSt(st);closeConn(conn);}public static void release(Connection conn , Statement st){closeSt(st);closeConn(conn);}private static void closeRs(ResultSet rs){try {if(rs != null){rs.close();}} catch (SQLException e) {e.printStackTrace();}finally{rs = null;}}private static void closeSt(Statement st){try {if(st != null){st.close();}} catch (SQLException e) {e.printStackTrace();}finally{st = null;}}private static void closeConn(Connection conn){try {if(conn != null){conn.close();}} catch (SQLException e) {e.printStackTrace();}finally{conn = null;}} }

转载于:https://blog.51cto.com/357712148/2152217

总结

以上是生活随笔为你收集整理的Properties文件读取学习笔记的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。