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. SVN上传文件提示磁盘空间不足的问题

    SVN上传文件大于100M,提示:Commit failed (details follow):While preparing 'E:\AFCData\tjsc20db_table.sql' for ...

  2. iOS开发UI篇—核心动画(转场动画和组动画)

    转自:http://www.cnblogs.com/wendingding/p/3801454.html iOS开发UI篇—核心动画(转场动画和组动画) 一.转场动画简单介绍 CAAnimation的 ...

  3. UnicodeToGB2312

    http://www.myluoluo.com/unicodetogb2312.love 你是否遇到类似于:\u5355\u4f4d之类的让人纠结的字符? 一个JS文件中一堆一堆的全都是这种,分析起来 ...

  4. 关于Java导出100万行数据到Excel的优化方案

    1>场景 项目中需要从数据库中导出100万行数据,以excel形式下载并且只要一张sheet(打开这么大文件有多慢另说,呵呵). ps:xlsx最大容纳1048576行 ,csv最大容纳1048 ...

  5. Model Validation in ASP.NET Web API

    Model Validation in ASP.NET Web API 原文:http://www.asp.net/web-api/overview/formats-and-model-binding ...

  6. 【转】有关Oracle随机字符串的生成方法及具体应用

    Oracle生成随机字符串的方法是通过dbms_random.string实现的. 1.dbms_random.string用法Oracle官方文档参考链接:http://download.oracl ...

  7. HDU1003 简单DP

    Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the ...

  8. mysql导入数据到oracle中

    mysql导入数据到oracle中. 建立Oracle表: CREATE TABLE "GG_USER" ( "USERID" BYTE) NOT NULL, ...

  9. Sizeof面试题

    sizeof()功能:计算数据空间的字节数1.与strlen()比较      strlen()计算字符数组的字符数,以"\0"为结束判断,不计算为'\0'的数组元素.       ...

  10. Android入门(四):链接接口组件和程序代码

    编写好layout中的接口组件之后,下一步就是编写控制接口组件的程序代码.上一章,我们使用了三种接口组件,在使用者输入性别和年龄之后点击“健康建议按钮”,程序会读取用户所填入的性别和年龄,然后显示判断 ...