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读写资源文件的更多相关文章

  1. Java读写资源文件类Properties

    Java中读写资源文件最重要的类是Properties 1) 资源文件要求如下: 1.properties文件是一个文本文件 2.properties文件的语法有两种,一种是注释,一种属性配置.  注 ...

  2. JAVA加载Properties配置资源文件

    JAVA加载Properties配置资源文件 制作人:全心全意 配置文件(资源文件):以properties作为拓展名的文件 Java代码是如何加载properties文件的? 必须使用Propert ...

  3. Properties读取资源文件的四种方法

    package com.action; import java.io.InputStream; import java.util.Locale; import java.util.Properties ...

  4. 使用Properties读写属性文件

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; /*Prop ...

  5. redis-config.properties属性资源文件

    redis.host=192.168.200.128redis.port=6379redis.pass=redis.database=0redis.maxIdle=300redis.maxWait=3 ...

  6. 读取web应用下的资源文件(例如properties)

    package gz.itcast.b_resource; import java.io.IOException; import java.io.InputStream; import java.ut ...

  7. JavaWeb基础: 获取资源文件

    Web工程在编译构建完毕以后,需要部署到Tomcat上运行,资源的硬盘路径也会随着改变.要想对资源文件进行读写操作需要获取其硬盘地址,在Web工程中通常通过ServletContext/ClassLo ...

  8. .NET MVC4 实训记录之五(访问自定义资源文件)

    .Net平台下工作好几年了,资源文件么,大多数使用的是.resx文件.它是个好东西,很容易上手,工作效率高,性能稳定.使用.resx文件,会在编译期动态生成已文件名命名的静态类,因此它的访问速度当然是 ...

  9. 【Java EE 学习 35 上】【strus2】【类型转换器】【struts2和Servlet API解耦】【国际化问题】【资源文件乱码问题已经解决】

    一.类型转换器 1.在动作类action中,声明和表单中name属性的值同名的属性,提供get和set方法,struts2就可以通过反射机制,从页面中获取对应的内容 package com.kdyzm ...

随机推荐

  1. css渐变/背景

    1.线性渐变(gradient变化) linear-gradient线性渐变指沿着某条直线朝一个方向产生渐变效果. 上图是从黄色渐变到绿色 background:linear-gradient( to ...

  2. 在Fedora 23 Server和Workstation上安装LAMP(Linux, Apache, MariaDB和PHP)

    在安装LAMP之前,建议先更新系统包$ sudo dnf update 第一步:安装Apache Web服务器1.在Fedora 23安装Apache,你可以运行下面的命令:$ sudo dnf in ...

  3. TcpClient

    public class TcpClientSession { protected TcpClient Client { get; set; } /// <summary> /// 远程地 ...

  4. VirtualBox安装linux增强工具报错

    错误提示: Building the OpenGL support module                         [FAILED] 解决办法 cd /media/VBOXADDITIO ...

  5. 使用微信 SDK 上传图片到七牛

    总体思路是:在微信下选好图片后将图片上传到微信服务器,在后端使用微信服务器返回的图片 serverId 加上调用接口的 ApiTicket 通过七牛的 fetch 接口向微信服务器下载多媒体文件的接口 ...

  6. CentOS 7 上搭建LNMP环境

    (转自美团云知识库Chris) 简介 LNMP是Linux.Nginx.MySQL(MariaDB)和PHP的缩写,这个组合是最常见的WEB服务器的运行环境之一.本文将带领大家在CentOS 7操作系 ...

  7. return 和 echo 的小坑

    在写项目的时候,有好几次遇到过同样的问题,控制器里面返回的json在前台无显示,利用console.log()总是显示这样的现象 数据库操作成功,却没有返回值. 原因是在控制器返回的使用使用了 ret ...

  8. 利用js制作异步验证ajax方法()

    如何利用js写ajax异步验证.代码如下: window.onload = function(){ var name = document.getElementById('register-name- ...

  9. offsetParent和parentNode区别

    offsetParent用的最普遍的就是来计算元素在页面中的位置,前面的日志理讲了 通过getBoundingClientRect() 来获取页面中元素的位置,不过这只支持最新的浏览器,如果要兼容像O ...

  10. javascript预加载和延迟加载

    延迟加载javascript,也就是页面加载完成之后再加载javascript,也叫on demand(按需)加载,一般有一下几个方法: What can your tired old page, o ...