Java 文件IO,配置文件的应用,properties类

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

引入问题

当我们在使用数据库的时候通常有如下需求。

连接数据库需要IP地址用户名密码。

我们不可能会在JAVA源程序中把连接数据库的IP用户名密码写死通常会写在文件中也就是properties文件比如创建如下properties文件

于是我们会把连接数据库的信息写在这个文件中

 直接读取Properties配置文件

编写如下程序直接读取Properties配置文件信息

import com.mysql.cj.result.Field;

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException ,ClassNotFoundException{
//        bufferedReader封装filereader
        BufferedReader bufferedReader = new BufferedReader(new FileReader("src/mysql_info.properties"));
//      读取一行
        String s = bufferedReader.readLine();
        while (s!=null)
        {
//            显示
            System.out.println(s);
            s = bufferedReader.readLine();
        }
//        关闭
        bufferedReader.close();
    }
}

读取效果如下

 但是我们通常只需要信息不需要关键字于是代码修改如下

import com.mysql.cj.result.Field;

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException ,ClassNotFoundException{
//        bufferedReader封装filereader
        BufferedReader bufferedReader = new BufferedReader(new FileReader("src/mysql_info.properties"));
//      读取一行
        String s = bufferedReader.readLine();
        while (s!=null)
        {
//            显示
            String[] split = s.split("=");
//            split[0]是关键字,splic[1]是值
            System.out.println(split[0]+"值为"+split[1]);
            s = bufferedReader.readLine();
        }
//        关闭
        bufferedReader.close();
    }
}

显示效果如下

使用Properties类读取配置文件

直接读取Properties配置文件有一个不好的一点是读取的数据是按照顺序读取的当我们想要单独的获取某一个关键字的值就显得比较麻烦而且当我们使用程序修改配置文件中的关键字的值也会非常的麻烦Properties类就很好的解决了这个问题。

Properties类介绍

Properties类常用方法

 

 使用properties类读取mysql_info.properties配置文件数据到程序

代码如下

import com.mysql.cj.result.Field;

import java.io.*;
import java.util.Properties;

public class Main {
    public static void main(String[] args) throws IOException ,ClassNotFoundException{
//        创建properties对象
        Properties properties = new Properties();
//        加载配置文件中的
        properties.load(new FileReader("src/mysql_info.properties"));
//        遍历显示 显示的方式就是屏幕显示(标准输出),当然也可以文件输出
        properties.list(System.out);
//        文件输出
//        properties.list(new PrintStream("a.txt"));
        System.out.println("------------------------");
//        根据键值获取值
        String ip = properties.getProperty("ip");
        System.out.println(ip);
        String username = properties.getProperty("username");
        System.out.println(username);
//      通过properties类获取配置文件更加的方便

    }
}

当然通过properties类可以修改配置文件关键字的数据.

就比如修改配置文件中的关键字username=山野

代码如下

import com.mysql.cj.result.Field;

import java.io.*;
import java.util.Properties;

public class Main {
    public static void main(String[] args) throws IOException ,ClassNotFoundException,FileNotFoundException{
//        创建properties对象
        Properties properties = new Properties();
//        加载配置文件中的
        properties.load(new FileReader("src/mysql_info.properties"));

//        修改配置关键字username为山野
        Object o = properties.setProperty("username", "山野");
//        正式写入文件
        properties.store(new FileOutputStream("src/mysql_info.properties"),null);
//        加载显示
        properties.list(System.out);


    }
}

执行效果如下

配置文件中的username更改了

使用properties进行程序的配置文件修改更加的方便。

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Java