zoukankan      html  css  js  c++  java
  • 【Java】Properties 配置信息类

    Properties 配置信息类

    Properties 是HashTable的子类,该对象用于处理属性文件

    由于属性文件的Key、Value都是字符串类型,所以Properties里的Key和Value也一样是String

    存取数据时使用:

    setProperty(String k,String v)

    getProperty(String k)

    要注意配置文件,xxx.properties要放在当前项目的目录下

    可以看到文件的信息已经可以被读取到

     文件流的异常和加载方法的异常直接使用抛出处理

     1 public class PropertiesTest {
     2     public static void main(String[] args) throws IOException {
     3         // 创建配置信息对象
     4         Properties properties = new Properties();
     5 
     6         // 创建文件流对象,指定文件
     7         FileInputStream inputStream = new FileInputStream("jdbc.properties");
     8 
     9         // 加载流对象的信息
    10         properties.load(inputStream);
    11 
    12         // 读取配置信息
    13         String username = properties.getProperty("username");
    14         System.out.println(username);
    15 
    16         String password = properties.getProperty("password");
    17         System.out.println(password);
    18     }
    19 }

    Properties文件

    username = root
    password = 123456
    driver = com.mysql.jdbc.Driver
    url = jdbc:mysql://localhost:3306/mysql
  • 相关阅读:
    char类型细节
    Hibernate面试题
    线程
    IO流
    集合
    链表相关的一点东西
    正则表达式学习
    python中的变量域问题
    python的输出和输入形式
    python mutable 和 immutable
  • 原文地址:https://www.cnblogs.com/mindzone/p/12743479.html
Copyright © 2011-2022 走看看