Properties读写资源文件
下面是文件操作的代码:
package com.liuyazhuang.properties.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import com.liuyazhuang.utl.StringUtils;
public class PropertiesUtil {
// 读取资源文件,并处理中文乱码
public static String readPropertiesFile(String filename,String key) {
if(StringUtils.isEmpty(filename.trim())||StringUtils.isEmpty(key.trim())) return null;
Properties properties = new Properties();
try {
InputStream inputStream = new FileInputStream(filename);
properties.load(inputStream);
inputStream.close(); // 关闭流
} catch (IOException e) {
e.printStackTrace();
}
String value = properties.getProperty(key);
try {
value = new String(value.getBytes("ISO-8859-1"), "UTF-8"); // 处理中文乱码
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
}
// 读取XML文件,并处理中文乱码
public static String readPropertiesFileFromXML(String filename,String key) {
if(StringUtils.isEmpty(filename.trim())||StringUtils.isEmpty(key.trim())) return null;
Properties properties = new Properties();
try {
InputStream inputStream = new FileInputStream(filename);
properties.loadFromXML(inputStream);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return properties.getProperty(key);
}
// 写资源文件,含中文
public static void writePropertiesFile(String filename,String key,String value) {
if(StringUtils.isEmpty(filename.trim())||StringUtils.isEmpty(key.trim())||StringUtils.isEmpty(value.trim())) return;
Properties properties = new Properties();
try {
OutputStream outputStream = new FileOutputStream(filename);
properties.setProperty(key, value);
properties.store(outputStream, null);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 写资源文件到XML文件,含中文
public static void writePropertiesFileToXML(String filename,String key,String value) {
if(StringUtils.isEmpty(filename.trim())||StringUtils.isEmpty(key.trim())||StringUtils.isEmpty(value.trim())) return;
Properties properties = new Properties();
try {
OutputStream outputStream = new FileOutputStream(filename);
properties.setProperty(key, value);
properties.storeToXML(outputStream, null);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//main方法测试
public static void main(String[] args) {
writePropertiesFile("d:/test.properties", "hello", "world");
System.out.println(readPropertiesFile("d:/test.properties", "hello"));
}
}
Properties读写资源文件的更多相关文章
- Java读写资源文件类Properties
Java中读写资源文件最重要的类是Properties 1) 资源文件要求如下: 1.properties文件是一个文本文件 2.properties文件的语法有两种,一种是注释,一种属性配置. 注 ...
- JAVA加载Properties配置资源文件
JAVA加载Properties配置资源文件 制作人:全心全意 配置文件(资源文件):以properties作为拓展名的文件 Java代码是如何加载properties文件的? 必须使用Propert ...
- Properties读取资源文件的四种方法
package com.action; import java.io.InputStream; import java.util.Locale; import java.util.Properties ...
- 使用Properties读写属性文件
import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; /*Prop ...
- redis-config.properties属性资源文件
redis.host=192.168.200.128redis.port=6379redis.pass=redis.database=0redis.maxIdle=300redis.maxWait=3 ...
- 读取web应用下的资源文件(例如properties)
package gz.itcast.b_resource; import java.io.IOException; import java.io.InputStream; import java.ut ...
- JavaWeb基础: 获取资源文件
Web工程在编译构建完毕以后,需要部署到Tomcat上运行,资源的硬盘路径也会随着改变.要想对资源文件进行读写操作需要获取其硬盘地址,在Web工程中通常通过ServletContext/ClassLo ...
- .NET MVC4 实训记录之五(访问自定义资源文件)
.Net平台下工作好几年了,资源文件么,大多数使用的是.resx文件.它是个好东西,很容易上手,工作效率高,性能稳定.使用.resx文件,会在编译期动态生成已文件名命名的静态类,因此它的访问速度当然是 ...
- 【Java EE 学习 35 上】【strus2】【类型转换器】【struts2和Servlet API解耦】【国际化问题】【资源文件乱码问题已经解决】
一.类型转换器 1.在动作类action中,声明和表单中name属性的值同名的属性,提供get和set方法,struts2就可以通过反射机制,从页面中获取对应的内容 package com.kdyzm ...
随机推荐
- JavaScript 回车 焦点切换(摘抄)
<!-- 这是回车转换行的代码段--> <script language='javascript' for='document' event='onkeydown'> if(e ...
- 关于rem自适应的一点研究
参考地址:http://m.ctrip.com/html5/ https://www.amazon.cn/ rem是相对于html根元素的一个单位.rem是px的16倍,即1rem = 16px;除了 ...
- OOP组合和继续的优缺点
—— 详解继承与组合的优缺点 组合与继承都是提高代码可重用性的手段.在设计对象模型时,可以按照语义来识别类之间的组合关系和继承关系.在有些情况下,采用组合关系或者继承关系能完成同样的任务,组合和继 ...
- 利用DreamweaverCS5制作一个含有动态标题的教程
DreamweaverCS5怎么制作一个含有动态标题?做一个网页就先要做一个标题,一个好标题会让网页让人印象深刻,有动态的标题会让网页更生动,下面我就介绍一下怎么制作一个含有动态的标题 做一个网页 ...
- poj3461Oulipo
Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...
- 【noip2012提高组】国王游戏
恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右 手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排 成一排,国王站在队伍的最前面. ...
- Android 技巧记录
1.取消EditText自动获取焦点 在项目中,一进入一个页面, EditText默认就会自动获取焦点,弹出输入法界面,很不友好.那么如何取消这个默认行为呢? 解决之道:在EditText的父级控件中 ...
- Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Chinese_PRC_CI_AS" in the equal to operation.
Scenario : 这个问题是我的存储过程中用到临时表时发生的. 应该是sql server 服务器的排序规则 (SQL_Latin1_General_CP1_CI_AS ) 与数据库的排序规则(C ...
- 960 grid 分析
960 网格系统的构造如下:页面总宽度 960px12 栏布局, 每栏 60px每栏两边保留 10px 的外边距, 相当于 20px 的槽内容区域总宽度是 940px 960 布局无疑是非常好的网格系 ...
- C语言的本质(3)——整数的本质与运算
C语言的本质(3)--整数的本质与运算 计算机存储的最小单位是字节(Byte),一个字节通常是8个bit.C语言规定char型占一个字节的存储空间.如果这8个bit按无符号整数来解释,则取值范围是0~ ...