Java中读写资源文件最重要的类是Properties

1) 资源文件要求如下:

1、properties文件是一个文本文件
2、properties文件的语法有两种,一种是注释,一种属性配置。
 注    释:前面加上#号
 属性配置:以“键=值”的方式书写一个属性的配置信息。
3、properties文件的一个属性配置信息值可以换行,但键不可以换行。值换行用“\”表示。
4、properties的属性配置键值前后的空格在解析时候会被忽略。
5、properties文件可以只有键而没有值。也可以仅有键和等号而没有值,但无论如何一个属性配置不能没有键。
eg:
正确的资源文件格式为:
 
 

2) 功能大致如下:

1. 读写Properties文件
2. 读写XML文件
3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要符合key=value格式即可.
Properties能读取以key,value存储的任何格式文件,看一下他的类结构就知道为什么了
从上面的类结构图可以看出,它继承了Hashtable并实现了Map接口
 
3)代码演示:
 package com.ifly.myhome.test;

 import java.io.File;
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; public class PropertiesMyTest
{ public static void main(String[] args)
{ String readfile = "e:" + File.separator + "readfile.properties";
String writefile = "e:" + File.separator + "writefile.properties";
String readxmlfile = "e:" + File.separator + "readxmlfile.xml";
String writexmlfile = "e:" + File.separator + "writexmlfile.xml";
String readtxtfile = "e:" + File.separator + "readtxtfile.txt";
String writetxtfile = "e:" + File.separator + "writetxtfile.txt"; readPropertiesFile(readfile); //读取properties文件
writePropertiesFile(writefile); //写properties文件
readPropertiesFileFromXML(readxmlfile); //读取XML文件
writePropertiesFileToXML(writexmlfile); //写XML文件
readPropertiesFile(readtxtfile); //读取txt文件
writePropertiesFile(writetxtfile); //写txt文件
} //读取资源文件,并处理中文乱码
public static void readPropertiesFile(String filename)
{
Properties properties = new Properties();
try
{
InputStream inputStream = new FileInputStream(filename);
properties.load(inputStream);
inputStream.close(); //关闭流
}
catch (IOException e)
{
e.printStackTrace();
}
String username = properties.getProperty("username");
String passsword = properties.getProperty("password");
String chinese = properties.getProperty("chinese");
try
{
chinese = new String(chinese.getBytes("ISO-8859-1"), "GBK"); // 处理中文乱码
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
System.out.println(username);
System.out.println(passsword);
System.out.println(chinese);
} //读取XML文件,并处理中文乱码
public static void readPropertiesFileFromXML(String filename)
{
Properties properties = new Properties();
try
{
InputStream inputStream = new FileInputStream(filename);
properties.loadFromXML(inputStream);
inputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
String username = properties.getProperty("username");
String passsword = properties.getProperty("password");
String chinese = properties.getProperty("chinese"); //XML中的中文不用处理乱码,正常显示
System.out.println(username);
System.out.println(passsword);
System.out.println(chinese);
} //写资源文件,含中文
public static void writePropertiesFile(String filename)
{
Properties properties = new Properties();
try
{
OutputStream outputStream = new FileOutputStream(filename);
properties.setProperty("username", "myname");
properties.setProperty("password", "mypassword");
properties.setProperty("chinese", "中文");
properties.store(outputStream, "author: shixing_11@sina.com");
outputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
} //写资源文件到XML文件,含中文
public static void writePropertiesFileToXML(String filename)
{
Properties properties = new Properties();
try
{
OutputStream outputStream = new FileOutputStream(filename);
properties.setProperty("username", "myname");
properties.setProperty("password", "mypassword");
properties.setProperty("chinese", "中文");
properties.storeToXML(outputStream, "author: shixing_11@sina.com");
outputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
} }
运行本程序所需的资源文件,我是放在E盘根目录,如E:/readfile.properties
1. readfile.properties
username=kh
password=kh
chinese=谓语
2. writefile.properties
#author: shixing_11@sina.com
#Fri May 28 22:19:44 CST 2010
password=kh
chinese=\u8C13\u8BED
username=kh

3. readxmlfile.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>

<entry key="password">mypassword</entry>

<entry key="chinese">中文</entry>

<entry key="username">myname</entry>

</properties>

4. writexmlfile.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>

<entry key="password">kh</entry>

<entry key="chinese">中文</entry>

<entry key="username">kh</entry>

</properties>

5. readtxtfile.txt    

           username=kh
           password=kh
           chinese=中文
6. writetxtfile.txt
 
           password=kh
           chinese=/u4E2D/u6587
           username=kh
 
4)Properties获取数据乱码解决

1.原因

Properties调用load(InputStream)时,读取文件时使用的默认编码为ISO-8859-1;当我们讲中文放入到properties文件中,通过getProperty(key)获取值时,取到得数据是ISO-8859-1格式的,但是ISO-8859-1是不能识别中文的。

2.解决方法

通过getProperty()获取的数据data既然是ISO-8859-1编码的,就通过data.getByte(“iso-8859-1”)获取获取,使用new String(data.getByte(“iso-8859-1”),”UTF-8”)进行转换。当然properties文件的编码类型需要和new String(Byte[],charst)中的第二个参数的编码类型相同

 
 

Java读写资源文件类Properties的更多相关文章

  1. Java 从资源文件(.properties)中读取数据

    在Java工程目录src下,创建一个后缀为.properties的文件,例如db.properties 文件中的内容如下(键=值): name=mk age=123 address=China 在程序 ...

  2. Properties读写资源文件

    Java中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XML文件 3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要 ...

  3. Eclipse中建立Maven项目后,Java Resources资源文件下没有src/main/java文件夹

    当建立好一个Maven项目后,在Java Resources资源文件夹下没有看到src/main/java文件夹,然后手动去创建Source Folder时,提示该文件已存在,如图: 有一个解决办法: ...

  4. java读取资源文件(Properties)

    四步: java代码 //new一个读取配置文件 Properties properties=new Properties(); //获取文件路径 String path=request.getSer ...

  5. java读写excel文件( POI解析Excel)

    package com.zhx.base.utils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi ...

  6. java web 资源文件读取

    前提:假设web应用test(工程名) webapps下面有一资源文件test.html 规则:在获取资源时一般使用的是相对路径,以符号/开头,而 / 代表什么取决于这个地址给谁使用.服务器使用时,/ ...

  7. Android 资源文件local.properties使用以及Gradle文件中的值、Manifests文件中的值

    这篇也是因为Gradle存储密钥问题一路填坑总结的,期初连.properties创建都有疑问 因为当时是在Android下查看新建的properties一直没法看到 因为Gradle Scripts是 ...

  8. 《Java知识应用》Java读写DBF文件

    1. 准备: Jar包下载:链接: https://pan.baidu.com/s/1Ikxx-vkw5vSDf9SBUQHBCw 提取码: 7h58 复制这段内容后打开百度网盘手机App,操作更方便 ...

  9. 【转】利用Eclipse编辑中文资源文件(application_zh_CN.properties )

    既然生为中国人,就没有什么好抱怨的了,遇到编码的问题,那只有解决它了. 如果经常使用Struts,并做过国际化操作的人来说,对于中文资源文件的处理应该不会感到陌生的.比如下面两个文件,一个是英文的,一 ...

随机推荐

  1. .NET 清理非托管资源

    Dispose 类型的 Dispose 方法应释放它拥有的所有资源.它还应该通过调用其父类型的 Dispose 方法释放其基类型拥有的所有资源.该父类型的 Dispose 方法应该释放它拥有的所有资源 ...

  2. 网站开发HTML部分课堂小结

    网页分为静态网页和动态网页两种 常用的是动态网页 静态网页修改数据是需要修改源代码,动态网页通过后台网页就可以修改静态网页有:HTML 内容(Hyper Text Markup Language 超文 ...

  3. iOS 状态栏隐藏显示

    在 info.plist 文件中 配置 View controller-based status bar appearance 对应的值为 NO 这样可以使用 application 对象来设置 状态 ...

  4. LATTICE USB下载线接口说明

    这节知识摘抄于网络,仅用几张图片来显示,提醒Usb下载线的连接方式. 最后贴出个人使用的8线的USB下载器的连接方式: 再补充一点,应为彩色杜邦线与USB下载器是活口连接,两个可以分开,再连接的时候, ...

  5. Designing IP-Based Video Conferencing Systems: Dealing with Lip Synchronization(唇音同步)

    转自:http://www.ciscopress.com/articles/article.asp?p=705533&seqNum=6 Correlating Timebases Using ...

  6. [译]:Orchard入门——部件管理

    原文链接:Managing Widgets 在Orchard中,部件是可以加入到当前当前主题任何位置或区域(如侧栏sidebar或底部区域footer)的UI块(如:HTML)或代码部分(如:内容部分 ...

  7. GO语言中json与map的转换

    直接上代码(需要引入encoding/json包) // 当前程序的包名 package main // 导入其它的包 import ( "encoding/json" " ...

  8. 基于hk2框架的功能测试Mock注入

    public Object getInstance(Class<?> clz){ return IocBean.get(clz.getName()); } public Object Mo ...

  9. android 多媒体数据库详解(转)

    转自:http://www.2cto.com/kf/201309/242876.html 主要分为几节: 1. Android的媒体文件内部是如何存储的? 2. Andoid的媒体文件如何获取? 3. ...

  10. 基于Proteus仿真的Arduino学习(1)——Arduino Uno最小系统及LED的简单使用

    一.前言:  A.Arduino简介 Arduino是由一个欧洲开发团队于2005年冬季开发.其成员包括Massimo Banzi.David Cuartielles.Tom Igoe.Gianluc ...