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 ...
随机推荐
- Reverse Key Indexes反向索引
Reverse Key Indexes反向索引A reverse key index is a type of B-tree index that physically reverses the by ...
- SQL Server DML(SELECT)常见用法(二)
1 引言 上篇讲到SQL Server中DML的基本使用方法,其中SELECT语句是最常用的语句,其功能强大,结构复杂,下面通过例子,具体介绍其使用方法. 2 SELECT查询语句 SELECT语 ...
- SVG-1
<rect>矩形 <circle>圆 <ellipse>椭圆 <line>直线 <polyline>折线 <polygon>标签 ...
- linux遇见的问题
我在/usr 文件夹下把hadoop-1.2.1文件夹改名为hadoop,不过hadoop已经存在了.这样就覆盖了.可是课件被占用了,可见没有真的删除掉. 怎么把消失的hadoop真正删除呢?
- android开发MD5加密工具类(一)
MD5加密工具类整理: package com.gzcivil.utils; import java.io.UnsupportedEncodingException; import java.secu ...
- 学习Oracle一个星期以来的总结
公司开发部门主要分2部分:.net开发和Oracle PL\SQL开发.刚入职的我被分到Oracle PL\SQL组了.Oracle是比SQL Server更大的数据库应用,我在学校只接触过SQL S ...
- Ugly Number,Ugly Number II,Super Ugly Number
一.Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are po ...
- 关于Git和Github
英文原文:Ten Things You Didn't Know Git And GitHub Could Do Git 和 GitHub 都是非常强大的工具.即使你已经使用他们很长时间,你也很有可能不 ...
- 容易上手-类似ERP系统 简单特效
今天大概简单写一个效果, 这个效果 很容易 上手的: html: <style type="text/css">.menu_list ul{display:none;} ...
- PHP简易计算器方法1
<?phpheader("content-type:text/html;charset=utf-8");session_start();?><!DOCTYPE h ...