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. <select> 默认选中

    不多bb,直接上代码 html代码: <li><span>所属类别</span> <select id="cate" name=" ...

  2. android系统中自带的一些ThemeStyle

    1 android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen" 布局页面最上面 不会显示  and ...

  3. Word2vec 模型载入(tensorflow)

    opts = Options() with tf.Graph().as_default(), tf.Session() as session: model = Word2Vec(opts, sessi ...

  4. form.submit(回调函数)——引用jq-form.js

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  5. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  6. LeetCode: Largest Rectangle in Histogram(直方图最大面积)

    http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...

  7. 管理node的版本

    检查当前node的版本 node -v 清除npm cache sudo npm cache clean -f 安装n模块 sudo npm install -g n 切换到别的版本,比如 v4.4. ...

  8. suse最小化安装

    Open suse 图形安装 用虚拟机vmware70 或者是Oracle VM VirtualBox安装 .镜像文件就是SLED-10-x86_64-DVD1 根据虚拟机设置吧镜像文件装入虚拟机中 ...

  9. IOC装配Bean(XML方式)

    Spring框架Bean实例化的方式 提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 无参数构造方法的实例化 <!-- 默认情况下使用的就是 ...

  10. android 无限循环的viewpager

    思路 例如存在 A -B -C 需要在viewpager滑动时无限循环 1.我们可以设计 C' A B C A'  C'与C相同,A'与A相同 2.滑动到A'时,则index回到1 3.滑动到C'时, ...