代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
操作属性文件
作者:
/ 发布于
2011/1/14
/
484
<div><span style="color: #000000"> </span><span style="color: #008000">/*</span><span style="color: #008000"> 操作属性文件,可以为我们的程序带来更方便的移植性,下面是一个示例,可以读、写、更改属性 读采用了两种方式,一种是采用Properties类,另外一种是采用资源绑定类ResourceBundle类, 下面是源程序,里面有详细的注释: </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.io.FileInputStream; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.io.FileOutputStream; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.io.InputStream; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.util.Properties; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.util.ResourceBundle; </span><span style="color: #008000">/**</span><span style="color: #008000"> *对属性文件(xx.properties)的操作 *注:属性文件一定要放在当前工程的根目录下,也就是放在与src目录在同一个目录下(我的JDevelop *是这样的) </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> OperatePropertiesFile { </span><span style="color: #0000ff">public</span><span style="color: #000000"> OperatePropertiesFile() { } </span><span style="color: #008000">/**</span><span style="color: #008000"> *采用Properties类取得属性文件对应值 *@parampropertiesFileNameproperties文件名,如a.properties *@parampropertyName属性名 *@return根据属性名得到的属性值,如没有返回"" </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> String getValueByPropertyName(String propertiesFileName,String propertyName) { String s</span><span style="color: #000000">=</span><span style="color: #000000">""</span><span style="color: #000000">; Properties p</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000"> Properties();</span><span style="color: #008000">//</span><span style="color: #008000">加载属性文件读取类</span><span style="color: #008000"> </span><span style="color: #000000"> FileInputStream in; </span><span style="color: #0000ff">try</span><span style="color: #000000"> { </span><span style="color: #008000">//</span><span style="color: #008000">propertiesFileName如test.properties</span><span style="color: #008000"> </span><span style="color: #000000"> in </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> FileInputStream(propertiesFileName);</span><span style="color: #008000">//</span><span style="color: #008000">以流的形式读入属性文件</span><span style="color: #008000"> </span><span style="color: #000000"> p.load(in);</span><span style="color: #008000">//</span><span style="color: #008000">属性文件将该流加入的可被读取的属性中</span><span style="color: #008000"> </span><span style="color: #000000"> in.close();</span><span style="color: #008000">//</span><span style="color: #008000">读完了关闭</span><span style="color: #008000"> </span><span style="color: #000000"> s</span><span style="color: #000000">=</span><span style="color: #000000">p.getProperty(propertyName);</span><span style="color: #008000">//</span><span style="color: #008000">取得对应的属性值</span><span style="color: #008000"> </span><span style="color: #000000"> } </span><span style="color: #0000ff">catch</span><span style="color: #000000"> (Exception e) { e.printStackTrace(); } </span><span style="color: #0000ff">return</span><span style="color: #000000"> s; } </span><span style="color: #008000">/**</span><span style="color: #008000"> *采用ResourceBundel类取得属性文件对应值,这个只能够读取,不可以更改及写新的属性 *@parampropertiesFileNameWithoutPostfixproperties文件名,不带后缀 *@parampropertyName属性名 *@return根据属性名得到的属性值,如没有返回"" </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> String getValueByPropertyName_(String propertiesFileNameWithoutPostfix,String propertyName) { String s</span><span style="color: #000000">=</span><span style="color: #000000">""</span><span style="color: #000000">; </span><span style="color: #008000">//</span><span style="color: #008000">如属性文件是test.properties,那此时propertiesFileNameWithoutPostfix的值就是test</span><span style="color: #008000"> </span><span style="color: #000000"> ResourceBundle bundel </span><span style="color: #000000">=</span><span style="color: #000000"> ResourceBundle.getBundle(propertiesFileNameWithoutPostfix); s</span><span style="color: #000000">=</span><span style="color: #000000">bundel.getString(propertyName); </span><span style="color: #0000ff">return</span><span style="color: #000000"> s; } </span><span style="color: #008000">/**</span><span style="color: #008000"> *更改属性文件的值,如果对应的属性不存在,则自动增加该属性 *@parampropertiesFileNameproperties文件名,如a.properties *@parampropertyName属性名 *@parampropertyValue将属性名更改成该属性值 *@return是否操作成功 </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">boolean</span><span style="color: #000000"> changeValueByPropertyName(String propertiesFileName,String propertyName,String propertyValue) { </span><span style="color: #0000ff">boolean</span><span style="color: #000000"> writeOK</span><span style="color: #000000">=</span><span style="color: #0000ff">true</span><span style="color: #000000">; Properties p</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000"> Properties(); InputStream in; </span><span style="color: #0000ff">try</span><span style="color: #000000"> { in </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> FileInputStream(propertiesFileName); p.load(in);</span><span style="color: #008000">// </span><span style="color: #000000"> in.close(); p.setProperty(propertyName,propertyValue);</span><span style="color: #008000">//</span><span style="color: #008000">设置属性值,如不属性不存在新建 </span><span style="color: #008000">//</span><span style="color: #008000">p.setProperty("testProperty","testPropertyValue");</span><span style="color: #008000"> </span><span style="color: #000000"> FileOutputStream out</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000"> FileOutputStream(propertiesFileName);</span><span style="color: #008000">//</span><span style="color: #008000">输出流</span><span style="color: #008000"> </span><span style="color: #000000"> p.store(out,</span><span style="color: #000000">""</span><span style="color: #000000">);</span><span style="color: #008000">//</span><span style="color: #008000">设置属性头,如不想设置,请把后面一个用""替换掉</span><span style="color: #008000"> </span><span style="color: #000000"> out.flush();</span><span style="color: #008000">//</span><span style="color: #008000">清空缓存,写入磁盘</span><span style="color: #008000"> </span><span style="color: #000000"> out.close();</span><span style="color: #008000">//</span><span style="color: #008000">关闭输出流</span><span style="color: #008000"> </span><span style="color: #000000"> } </span><span style="color: #0000ff">catch</span><span style="color: #000000"> (Exception e) { e.printStackTrace(); } </span><span style="color: #0000ff">return</span><span style="color: #000000"> writeOK; } } </span> </div>
试试其它关键字
操作属性
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3