Properties读写资源文件

2021/6/20 23:56:46

本文主要是介绍Properties读写资源文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Java中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XML文件 3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要符合key=value格式即可.
注意:资源文件中含有中文时的处理方法  1. 将中文字符通过工作转成utf8编码,可以通过Java自带的nativetoascii或Eclipse中的属性编辑器。 2. 直接调用 new String(youChineseString.getBytes("ISO-8859-1"), "GBK");或者new String(youChineseString.getBytes("ISO-8859-1"), "UTF-8");
附:WEB程序中加载资源文件的方法 Properties prop = null;  1. prop = Thread.currentThread().getContextClassLoader().getResourceAsStream("filename"); 2. prop = this.getClass().getClassLoader().getResourceAsStream("filename");

下面是文件操作的代码:
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读写资源文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程