properties基本用法
package control;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
public class TestMain {
//根据key读取value
public static String readValue(String filePath,String key) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
String value = props.getProperty (key);
System.out.println(key+value);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//读取properties的全部信息
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty (key);
System.out.println(key+Property);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//写入properties信息
public static void writeProperties(String filePath,String parameterName,String parameterValue) {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
prop.load(fis);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(parameterName, parameterValue);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "Update '" + parameterName + "' value");
} catch (IOException e) {
System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
}
}
public static void main(String[] args) {
readValue("info.properties","url");
writeProperties(");
readProperties("info.properties" );
System.out.println("OK");
}
properties基本用法的更多相关文章
- 读取properties文件以及properties的用法
package cn.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties ...
- Properties的用法
/** * Description:Properties是HashTable的子類,其存儲形式鍵值對. */ import java.util.*; public class PropertysTes ...
- Java properties文件用法
package com.suyang.properties; import java.io.FileInputStream; import java.io.FileNotFoundException; ...
- JAVA中properties基本用法
转载 源地址不详 java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式, ...
- java Properties的用法
Properties是一个特殊的Map,因为和IO流牵扯到了一块…… import java.io.BufferedReader;import java.io.File;import java.io. ...
- 关于Properties的用法的详细解释
如果不熟悉 java.util.Properties类,那么现在告诉您它是用来在一个文件中存储键-值对的,其中键和值是用等号分隔的.(如清单 1 所示).最近更新的java.util.Properti ...
- Android下用Properties保存程序配置
读写函数分别例如以下: import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Proper ...
- Android通过使用Properties保存配置
读写功能,如下面分别: import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Proper ...
- Java-Properties用法-入门
对于应用程序的配置,通常的做法是将其保存在独立的配置文件中,程序启动时加载,修改时保存.Java中Properties类就提供了这样一种机制,配置项以Key-Value的数据结构存储在文本文件中,扩展 ...
随机推荐
- Velocity动态拼接字符串
1.在全局定义一个变量: #set($varName = "") 2.拼接字符串病截取字符串: #foreach( $role in $adminUser.roles) #set( ...
- Django 模版中如何对主菜单进行选中?
问题描叙: 在实际模版中,返回的页面我们想要主菜单保持一种状态,这个主菜单进去的子页面,都需要保持主菜单选择状态 例如 主菜单是 课程, 下面展现的章节和视频页面时候, 课程这个主菜单在展现这个菜单下 ...
- 前端开发人员需要了解的CSS原理
转自http://web.jobbole.com/10011/ 一.浏览器的发展与CSS 网页浏览器主要通过HTTP协议连接网页服务器而取得网页,HTTP容许网页浏览器送交资料到网页服务器并且获取网页 ...
- python 爬取B站视频弹幕信息
获取B站视频弹幕,相对来说很简单,需要用到的知识点有requests.re两个库.requests用来获得网页信息,re正则匹配获取你需要的信息,当然还有其他的方法,例如Xpath.进入你所观看的视频 ...
- 【转】python入门指引
http://matrix.42qu.com/10757179 前言 其实我也不知道python怎么入门,由我来写这个真的不是很合适.我学python是直接找了dive into python来看.然 ...
- Sql Server的艺术(二) SQL复杂条件搜索
本次讲到where字句中经常用到的集中较为复杂的搜索条件,包括组合的查询条件.IN运算符.NOT运算符.LIKE运算符和相关通配符. 学习本节需要用到一下两张表: CREATE TABLE TEACH ...
- oralce plsql案例练习
以下plsql程序用的scott用户的dept,emp表. 案例1 --查询80,81,82,87年员工入职人数 set serveroutput on declare cursor cemp is ...
- 聊聊js里面容易忽视的一些东西(1)
JavaScript对象的创建方式 在JavaScript中,创建对象的方式包括两种:对象字面量和使用new表达式.对象字面量是一种灵活方便的书写方式,例如: 1 2 3 4 5 6 var o1 ...
- 使用hexo搭建个人博客
安装前提 node.js git 如果缺少以上条件,则前往相应的官网下载安装即可.. 安装hexo $ npm install hexo-cli -g 待安装完成后,执行相关命令查看hexo的信息. ...
- css选择器:nth-child()与:nth-of-type()的差异
:nth-child()和:nth-of-type()都是Css3中的伪类选择器,其作用相似却又不完全相同. 名词解释 :nth-child()选择器匹配其父元素的第n个子元素,不论元素类型. :nt ...