日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Properties文件读取学习笔记

發(fā)布時(shí)間:2025/5/22 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Properties文件读取学习笔记 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

第一種

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() {}/*** 構(gòu)造方法** @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(); // 關(guān)閉流} catch (IOException e) {e.printStackTrace();}return properties;}/*** 獲取某項(xiàng)文本內(nèi)容** @param key* @return*/public String getPro(String key) {if (prop.containsKey(key)) {return prop.getProperty(key);} else {System.out.println("獲取的鍵不存在");return "";}}/*** 寫入某項(xiàng)文本內(nèi)容** @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. 創(chuàng)建一個(gè)屬性配置對(duì)象Properties properties = new Properties();InputStream is = new FileInputStream("jdbc.properties");//使用類加載器,去讀取src底下的資源文件。 后面在servlet // InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");//導(dǎo)入輸入流。properties.load(is);//讀取屬性driverClass = properties.getProperty("driverClass");url = properties.getProperty("url");name = properties.getProperty("name");password = properties.getProperty("password");} catch (Exception e) {e.printStackTrace();}}/*** 獲取連接對(duì)象* @return*/public static Connection getConn(){Connection conn = null;try {Class.forName(driverClass);//靜態(tài)代碼塊 ---> 類加載了,就執(zhí)行。 java.sql.DriverManager.registerDriver(new Driver());//DriverManager.registerDriver(new com.mysql.jdbc.Driver());//DriverManager.getConnection("jdbc:mysql://localhost/test?user=monty&password=greatsqldb");//2. 建立連接 參數(shù)一: 協(xié)議 + 訪問的數(shù)據(jù)庫(kù) , 參數(shù)二: 用戶名 , 參數(shù)三: 密碼。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;}} }

轉(zhuǎn)載于:https://blog.51cto.com/357712148/2152217

總結(jié)

以上是生活随笔為你收集整理的Properties文件读取学习笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。