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读写资源文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求