Java读写资源文件类Properties

简介: Java读写资源文件类Properties

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

1.资源文件要求如下:

  • properties文件是一个文本文件
  • properties文件的语法有两种,一种是注释,一种属性配置。

    注 释:前面加上#号

    属性配置:以“键=值”的方式书写一个属性的配置信息。

  • properties文件的一个属性配置信息值可以换行,但键不可以换行。值换行用“\”表示。
  • properties的属性配置键值前后的空格在解析时候会被忽略。
  • properties文件可以只有键而没有值。也可以仅有键和等号而没有值,但无论如何一个属性配置不能没有键。

eg:

正确的资源文件格式为:

2.功能大致如下:

  1. 读写Properties文件
  2. 读写XML文件
  3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要符合key=value格式即可.

Properties能读取以key,value存储的任何格式文件,看一下他的类结构就知道为什么了

从上面的类结构图可以看出,它继承了Hashtable并实现了Map接口

3.代码演示:

package com.itbird.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)中的第二个参数的编码类型相同

目录
相关文章
|
6天前
|
Java 开发者
奇迹时刻!探索 Java 多线程的奇幻之旅:Thread 类和 Runnable 接口的惊人对决
【8月更文挑战第13天】Java的多线程特性能显著提升程序性能与响应性。本文通过示例代码详细解析了两种核心实现方式:Thread类与Runnable接口。Thread类适用于简单场景,直接定义线程行为;Runnable接口则更适合复杂的项目结构,尤其在需要继承其他类时,能保持代码的清晰与模块化。理解两者差异有助于开发者在实际应用中做出合理选择,构建高效稳定的多线程程序。
26 7
|
5天前
|
Java
【Java】Math、System、RunTime、BigDecimal类常用方法
【Java】Math、System、RunTime、BigDecimal类常用方法
|
3天前
|
安全 Java API
16 个最常用的 Java 实用程序类
【8月更文挑战第16天】
10 1
16 个最常用的 Java 实用程序类
|
6天前
|
存储 Java 数据库连接
Java类文件结构及类加载机制
该文章主要讨论了Java类文件的结构以及Java类的加载机制,并提到了双亲委派模型的相关内容。
Java类文件结构及类加载机制
|
11天前
|
存储 算法 Java
14 Java集合(集合框架+泛型+ArrayList类+LinkedList类+Vector类+HashSet类等)
14 Java集合(集合框架+泛型+ArrayList类+LinkedList类+Vector类+HashSet类等)
30 2
14 Java集合(集合框架+泛型+ArrayList类+LinkedList类+Vector类+HashSet类等)
|
11天前
|
Java
Java 基础语法-面试题(54-63道)(数组+类+包)
Java 基础语法-面试题(54-63道)(数组+类+包)
29 16
|
1天前
|
SQL Java Apache
实时计算 Flink版操作报错合集之使用parquet时,怎么解决报错:无法访问到java.uti.Arrays$ArrayList类的私有字段
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
|
4天前
|
Oracle 安全 Java
JDK8到JDK28版本升级的新特性问题之在Java 15及以后的版本中,密封类和密封接口是怎么工作的
JDK8到JDK28版本升级的新特性问题之在Java 15及以后的版本中,密封类和密封接口是怎么工作的
|
5天前
|
设计模式 人工智能 Java
Java 如何使用单例类
Java 如何使用单例类
5 1
|
5天前
|
前端开发 Java 编译器
【前端学java】java中的Object类和前端中的Object有什么区别(9)
【8月更文挑战第10天】java中的Object类和前端中的Object有什么区别
13 0
【前端学java】java中的Object类和前端中的Object有什么区别(9)