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 ...
随机推荐
- Intent组件
Android页面之间的跳转可以分为几类,比如有A和B两个页面,从A页面直接跳转到B页面,这样的跳转比较简单,或者从A页面跳转到B页面但是需要传送数据.在Android中,页面的跳转离不开Intent ...
- Knime 使用 初探
使用数据挖掘工具 Knime,分析某公司用户使用情况. 首先,打开csv文件数据,看到有以下门类: 时间.track id 歌曲名.用户行为.用户id.日期.snap_id 即歌曲门类 然后,打开Kn ...
- python正则表达式练习篇2
首先生成正则表达式练习的数据: #! /usr/bin/python from random import randint, choice from string import lowercase f ...
- python基础:映射和集合类型
python字典的迭代器遍历 字典有一个方法可以返回该字典的迭代器,这个方法就是: dict. iteritems() 当在字典中增加或者删除字典entry的时候,迭代器会失效的,类似于C++的stl ...
- git使用前配置
1. git config --global user.name "Trey" 2. git config --global user.email 164355949@qq.com ...
- mysql 建立索引的原则
建索引的几大原则 1. 最左前缀匹配原则,非常重要的原则,mysql会一直向右匹配直到遇到范围查询(>.<.between.like)就停止匹配,比如a 1="" an ...
- svn 查看某个时间段的记录
调出svn后,点击查找历史的按钮.
- ajax.request函数使用详解
Ajax.Request ? Ajax.Request( url, { method:method, parameters:para, postBody:xmlString, asynchrono ...
- distance.c
#include "stdio.h" #include "string.h" #include "math.h" #include &quo ...
- 《小猪CMS(PigCms)多用户微信营销服务平台系统V6.1完美破解至尊版带微用户管理CRM+微信支付》
<小猪CMS(PigCms)多用户微信营销服务平台系统V6.1完美破解至尊版带微用户管理CRM+微信支付> 之前发布了不少微赢的多用户微信网站源码,今天为我的小伙伴们准备的是功能非常强悍, ...