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"));
    }
}

Properties文件读取学习笔记
Properties文件读取学习笔记

第二种:

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;
        }
    }
}

猜你喜欢

转载自blog.51cto.com/357712148/2152217