Properties文件读取学习笔记
生活随笔
收集整理的這篇文章主要介紹了
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文件读取学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SqlServer注意事项总结,高级程序
- 下一篇: 使用 Redis 实现分布式速率限制