import java.io.*;
import java.util.Enumeration;
import java.util.Properties; /**
* 关于Properties类常用的操作
* @author xfm
* @version 1.0.0
*/
public class PropertiesUtil {
private final String prefix = "src/main/resources/";
private String filePath; public PropertiesUtil(String filePath) {
this.filePath = filePath;
} /**
* 根据Key读取Value
* @param filePath 文件名称
* @param key 键名
* @return String
*/
public static String getValueByKey(String filePath, String key) {
Properties pps = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
String value = pps.getProperty(key);
return value; } catch (Exception e) {
e.printStackTrace();
return null;
}
} public String getValueByKey(String key) {
String value = null;
if (key != null && !"".equals(key)) {
Properties pps = new Properties();
try {
// InputStream in = new BufferedInputStream(new FileInputStream(prefix+filePath));
InputStreamReader isr = new InputStreamReader(new FileInputStream(
prefix + filePath), "gbk");
pps.load(isr);
value = pps.getProperty(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return value;
} /**
* 读取Properties的全部信息
* @param filePath 文件路径
* @throws IOException IOException异常
*/
public static void getAllProperties(String filePath) throws IOException {
Properties pps = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
//得到配置文件的名字
Enumeration en = pps.propertyNames(); while (en.hasMoreElements()) {
String strKey = (String) en.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
} } public static void writeProperties(String filePath, String pKey, String pValue) throws IOException {
Properties pps = new Properties(); InputStream in = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
pps.load(in);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream out = new FileOutputStream(filePath);
pps.setProperty(pKey, pValue);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
pps.store(out, "Update " + pKey + " name");
}
}

关于Properties类常用的操作的更多相关文章

  1. Properties类和如何操作属性

    Properties类继承关系java.lang.Object  java.util.Dictionary<K,V>      java.util.Hashtable<Object, ...

  2. C#File类常用文件操作以及一个模拟的控制台文件管理系统

    重温一下C#中File类的一些基本操作: File类,是一个静态类,主要是来提供一些函数库用的. 使用时需要引入System.IO命名空间. 一.常用操作: 1.创建文件方法 //参数1:要创建的文件 ...

  3. Java中Properties类的操作

    知识学而不用,就等于没用,到真正用到的时候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用Java来写,外加 ...

  4. Java中Properties类的操作配置文件

    知识学而不用,就等于没用,到真正用到的时 候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用 Java来写, ...

  5. Java 中 Properties 类的操作

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

  6. Java中Properties类

    1 简介: JDK提供的java.util.Properties类继承自Hashtable类并且实现了Map接口,用map来存储key-value数据,所以存入的数据是无序的.其中键和值都是字符串类型 ...

  7. 对Java中properties类的理解

    转载于:https://www.cnblogs.com/bakari/p/3562244.html 一.Java Properties类 Java中有个比较重要的类Properties(Java.ut ...

  8. Java的Properties类使用

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

  9. 关于Java的Properties类

    Properties类 先来学习下Properties类吧. Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串. ...

随机推荐

  1. Git-远程仓库的使用

    Git修改远程仓库地址 1.修改命令 git remote set-url origin [url] 例如:$ git remote set-url origin gitlab@gitlab.chum ...

  2. AI_神经网络监督学习

    神经网络的神奇之处在哪? 所有神经网络创造出来的价值,都是由一种机器学习,称之为监督学习, 下面这些例子神经网络效果拔群,通过深度学习获利最多的是在线广告 技术的进步来源于计算机视觉和深度学习 例如: ...

  3. 爬取西刺网代理ip,并把其存放mysql数据库

    需求: 获取西刺网代理ip信息,包括ip地址.端口号.ip类型 西刺网:http://www.xicidaili.com/nn/ 那,如何解决这个问题? 分析页面结构和url设计得知: 数据都在本页面 ...

  4. python初识 - day4

    一.集合(set) 1.集合的定义 set 是一个无序的元素集合,支持并.交.差及对称差等数学运算, 但由于 set 不记录元素位置, 因此不支持索引.分片等类序列的操作. 2.集合的创建 大括号或 ...

  5. BSA Network Shell系列-runcmd/runscript命令

    runcmd和runscript ## 1 功能概述 runcmd/runscript:runcmd在一台或多台机器执行Network Shell命令(单个命令),而runscript执行的是脚本,二 ...

  6. 开发步骤Dubbo、spring mvc、springboot、SSM整合开发步骤

    一.Dubbo开发步骤: 链接:https://pan.baidu.com/s/1pMPO1kf 密码:9zaa 第一: 1.创建consumer工程2.在pom.xml文件下添加配置3.添加appl ...

  7. html页面不显示中文

    问题:HTML编辑中文后无法在网页上显示中文 原因:是适用的 Cufon字体包不支持中文造成的. 简单粗暴的解决方法:如果你本来打算是去掉Cufon,可以看一看<如何禁用Cufon功能>. ...

  8. sed进阶N;P;D

    案例 sed 的高级替换 $cat file1 why:1 why:2 3 4 5 why:6 why:7 8 why:9 $cat file2 why:1 why:2 3 4 5 why:6 why ...

  9. Macaca拓展自己控件的方法

    https://github.com/macacajs/wd.py/blob/3bc4334bcb68733cb230b59d6164110053fd1c16/tests/macaca/test_ut ...

  10. 淘宝地址爬取及UI展示

    淘宝地址爬取及UI展示 淘宝国家省市区街道获取 参考 foxiswho 的 taobao-area-php 部分代码,改由c#重构. 引用如下: Autofac MediatR Swagger Han ...