Java使用IO流读取配置文件

本文介绍了如何利用Java的IO流读取Properties配置文件,特别是在Web应用开发中,这种技术常用于读取数据库连接配置。文章通过示例展示了一个测试类,用于获取配置文件中的name和password,并讨论了在DAO层中使用此类实现JDBC连接的基类,该基类提供了灵活的SQL语句处理方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于配置文件Properties的概述

Properties 类表示了一个持久的属性集。
Properties 可保存在流中或从流中加载。
属性列表中每个键及其对应值都是一个字符串。
Properties父类是Hashtable
-属于双列集合,这个集合中的键和值都是字符串 Properties不能指定泛型
就是说对于Properties类型的文件,其内容都是按照键值对存放的,而且其键值都是字符串类型的数据
例如:


name=aa
password=bb

所以我们就可以通过IO流来将一个已经存在内容的Properties文件进行读取,获得文件内容里键对应的值
创建测试类读取name和password

package com.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Test{              
         
private static InputStream inStream=null;

        
private  
static Properties pro;       
//把代码放在静态代码块中,是为了在new对象时能够首先执行代码块
       
static{                  
try{
                           
//通过反射获取InputStream流
                           
//注意:db.properties
与Test.java
放在同一包下
                           
inStream=Test.class.getResourceAsStream("db.properties");
                         
//创建Properties对象
                          
Properties  pro=newProperties();

                           
//将输入流载入properties对象中

                           
pro.load(inStream);

                           


                  
}catch (IOException e) {

                           
e.printStackTrace();

                  
}

                          
}

        


        


        
public  void readProperties(){

                  
//获取Properties
文件中属性,并输出

                  
System.out.println(pro.getProperty("name"));

                  
System.out.println(pro.getProperty("password"));

                  


        
}
 
        
publicstatic void main(String[] args) {

                  
//在main()
方法中
调用readProperties()方法

                  
Testt=new Test();

                  
t.readProperties();

                  


        
}

结果
在这里插入图片描述
对于读取配置文件,我们在web应用开发中最常用来用于读取连接数据库的配置文件,下面做一个dao层的基础类实现连接数据库JDBC

package com.JdbcConnection.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

import static org.omg.CORBA.ORB.init;

public class BaseDao {

    static  {//静态代码块在类加载的时候执行、
        try {
            init();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

//定义私有变量, 对应配置文件里关于数据库的信息
    private  static String  driver;
    private  static String  url;
    private  static  String  user;
    private  static  String  password;


    public  static  void init() throws IOException {
        Properties properties = new Properties();
        InputStream re = BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
        //  这里和上面的例子一样,是我们通过类的.class方法获得类加载器.再通过getResourceAsStream获得配置文件的流输出
        properties.load(re);
//使用properties.load()方法,获得配置文件的流输入
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        properties.getProperty("password");
    }

    //获得数据库连接
    public  static Connection getConnection() throws ClassNotFoundException, SQLException {
        Connection connection=null;//创建一个connection对象
        Class.forName(driver);//加载驱动
        connection= DriverManager.getConnection(url,user,password);
        return  connection;//返回这个连接对象

    }
public  static ResultSet execute(Connection connection, PreparedStatement perpared, ResultSet res, String sql, Object[] params) throws SQLException {


       perpared=connection.prepareStatement(sql);//
    for (int i = 0; i < params.length; i++) {
        perpared.setObject(i+1,params[i]);
    }
   res = perpared.executeQuery();
    return  res;
}
//更新操作

    public static  int  execute(Connection  connection, PreparedStatement perpared,String sql,Object[] params) throws SQLException {
       int updateRows=0;
       connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            perpared.setObject(i+1,params[i]);

        }
        updateRows=perpared.executeUpdate();


        return updateRows;
    }
    //释放资源
    public static boolean closeResource(Connection connection,PreparedStatement perpared,ResultSet res) throws SQLException {
       boolean flag=true;
        if(res!=null){
            res.close();
            res=null;
            flag =false;
        }
        if(perpared!=null){
            perpared.close();
            perpared=null;
            flag=false;
        }
        if (connection!=null){
            connection.close();
            connection=null;
            flag=false;
        }
        return flag;
    }

}

这个基础类提供了在dao层其他业务的对数据库表进行操作的多个静态方法,尤其是对该类中的静态方法使用的可变参数,对其它类的sql语句的写入提供了灵活的方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值