一、properties文件

Properties文件是java中很常用的一种配置文件,文件后缀为“.properties”,属文本文件,文件的内容格式是“键=值”的格式,可以用“#”作为注释,java编程中用到的地方很多,运用配置文件,可以便于java深层次的解耦。

例如java应用通过JDBC连接数据库时,可以把数据库的配置写在配置文件 jdbc.properties:

driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/user
user=root
password=123456

这样我们就可以通过加载properties配置文件来连接数据库,达到深层次的解耦目的,如果想要换成oracle或是DB2,我们只需要修改配置文件即可,不用修改任何代码就可以更换数据库。

二、Properties类

java中提供了配置文件的操作类Properties类(java.util.Properties):

读取properties文件的通用方法:根据键得到value

    /**
* 读取config.properties文件中的内容,放到Properties类中
* @param filePath 文件路径
* @param key 配置文件中的key
* @return 返回key对应的value
*/
public static String readConfigFiles(String filePath,String key) {
Properties prop = new Properties();
try{
InputStream inputStream = new FileInputStream(filePath);
prop.load(inputStream);
inputStream.close();
return prop.getProperty(key);
}catch (Exception e) {
e.printStackTrace();
System.out.println("未找到相关配置文件");
return null;
}
}

把配置文件以键值对的形式存放到Map中:

    /**
* 把.properties文件中的键值对存放在Map中
* @param inputStream 配置文件(inputstream形式传入)
* @return 返回Map
*/
public Map<String, String> convertPropertityFileToMap(InputStream inputStream) {
try {
Properties prop = new Properties();
Map<String, String> map = new HashMap<String, String>();
if (inputStream != null) {
prop.load(inputStream);
Enumeration keyNames = prop.propertyNames();
while (keyNames.hasMoreElements()) {
String key = (String) keyNames.nextElement();
String value = prop.getProperty(key);
map.put(key, value);
}
return map;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

Properties类操作.properties配置文件方法总结的更多相关文章

  1. properties类的基本使用方法

    properties类的基本使用方法1.假设有“pp.properties”,内容有       age=22       2.java中用下面方法:   Properties   props   = ...

  2. java.util.Properties类的介绍-配置文件的读写【-Z-】

    简介:java.util.Properties是对properties这类配置文件的映射.支持key-value类型和xml类型两种. #打头的是注释行,Properties会忽略注释.允许只有key ...

  3. 基于Java Properties类设置本地配置文件

    一.Java Properties类介绍 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件, ...

  4. 利用Properties类关联相关配置文件

    文件目录 代码: package Lianxi;import java.io.FileInputStream;import java.io.FileNotFoundException;import j ...

  5. Properties类读写.properties配置文件

    package com.hzk.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFo ...

  6. 【转】ini载入保存类,操作INI配置文件方便的很

    /****************************************************************** * * ^_^ 恶猫 独门商标 挖哈哈 * * QQ:\> ...

  7. python 操作conf配置文件方法

    参考文章链接:https://blog.csdn.net/qq_23587541/article/details/85019610

  8. Properties类与配置文件

    //加载文件public static void testLoadProperties() throws Exception { Properties properties = new Propert ...

  9. day1 java基础回顾- Properties类与配置文件

    Properties配置文件说明 Properties类对应.properties文件.文件内容是键值对,键值对之间使用"="或空格隔开.开头是"#"的表示注释 ...

随机推荐

  1. touch的基本用法

    touch的基本功能是改变文件的时间戳,以下是三种常用用法: 1.创建一个空文件 以当前时间为时间戳创建一个空文件.$ touch touch.txt //touch.txt不存在 2.更改文件时间戳 ...

  2. ES6之箭头函数深入理解

    相对于普通函数的区别 新的书写方式 this 的改变 不能当构造函数 没有 prototype 属性 没有 arguments 对象 新的书写方式 书写方式很简单!直接看下图, 常规方式写一个函数 c ...

  3. iOS RSA (Objc)

    /* RSA.h @author: ideawu @link: https://github.com/ideawu/Objective-C-RSA */ #import <Foundation/ ...

  4. web.xml中classpath*:与classpath:的区别

    classpath对应src目录,该目录下的文件会在编译后被存放到WEB-INF文件夹下的classes目录. classpath:只会到你的class路径中查找配置文件,对于多个同名的配置文件,只会 ...

  5. UWP 后台保存图片

    在做UWP的时候,有一个需求,就是点击下载按钮,需要将当前页面中的Image控件中显示的图片保存下来. 既然聊到了下载图片,索性把添加图片也讲一下. 一:给Image控件添加图片 xaml代码: &l ...

  6. Stars in Your Window POJ - 2482

    错误记录: 题目说输入在int范围内,但是运算过程中可能超int:后来开了很多longlong就过了 #include<cstdio> #include<algorithm> ...

  7. A Refining Company LightOJ - 1036

    A Refining Company LightOJ - 1036 描述好长啊... 题意:在m*n的矩阵上,每一格摆一个向上或者向左的传送带(不能同时摆,只能摆一个).同时,每一格有两种物资Uran ...

  8. 18.3.2从Class上获取信息(方法)

    package d18_3_1; import java.lang.reflect.Method; import java.util.Arrays; /** * 获取Class对应类所包含的方法的四个 ...

  9. 当document.write 遇到外联script

    先来看个例子: <!DOCTYPE html> <html> <head> <title>测试 document.write</title> ...

  10. word2vec的Java源码【转】

    一.核心代码 word2vec.java package com.ansj.vec; import java.io.*; import java.lang.reflect.Array; import ...