Java中Properties的使用详解

这篇文章主要介绍了Java中Properties的使用使用详解的相关资料,需要的朋友可以参考下。
Java中有个比较重要的详解类Properties(Java.util.Properties),主要用于读取Java的使用配置文件,各种语言都有自己所支 持的详解配置文件,配置文件中很多变量是使用经常改变的,这样做也是详解为了方便用户,让用户能够脱离程序本身去修改相关的使用变量设置。今天,详解我们就开始Properties的亿华云使用使用。
Java中Properties的详解使用
Properties的文档说明:
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
Properties类的描述:
public class Properties extends Hashtable
测试的项目结构如下:

一、在huhx.properties文件中,使用我们为也方便,详解加入一条数据:
name=huhx二、使用将huhx.properties文件加载读取,详解得到相应的使用属性
Properties properties = new Properties(); FileInputStream fis = new FileInputStream("huhx.properties"); properties.load(fis); System.out.println(properties.get("name"));三、Properties的list方法的使用
PrintStream printStream = System.out; properties.list(printStream);list方法的具体代码:
public void list(PrintStream out) { out.println("-- listing properties --"); Hashtable h = new Hashtable(); enumerate(h); for (Enumeration e = h.keys() ; e.hasMoreElements() ;) { String key = (String)e.nextElement(); String val = (String)h.get(key); if (val.length() > 40) { valval = val.substring(0, 37) + "..."; } out.println(key + "=" + val); } }四、Properties的store方法的企商汇使用
OutputStream outputStream = new FileOutputStream("huhx.txt"); properties.store(outputStream, "comments");五、Properties的storeToXML方法的使用
OutputStream outputStream2 = new FileOutputStream("huhx.xml"); properties.storeToXML(outputStream2, "comments");六、最终生成的文件如下:
huhx.txt: #comments #Thu May 19 19:19:36 CST 2016 name=huhx huhx.xml: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>comments</comment> <entry key="name">huhx</entry> </properties>友情链接,PropertiesTest.java:
package com.huhx.linux; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.PrintStream; import java.util.Properties; public class PropertiesTest { public static void main(String[] args) throws Exception {// 一般Properties的使用
Properties properties = new Properties(); FileInputStream fis = new FileInputStream("huhx.properties"); properties.load(fis); System.out.println(properties.get("name"));// 以下是测试的部分
PrintStream printStream = System.out; properties.list(printStream); OutputStream outputStream = new FileOutputStream("huhx.txt"); properties.store(outputStream, "comments"); OutputStream outputStream2 = new FileOutputStream("huhx.xml"); properties.storeToXML(outputStream2, "comments"); } }
以上所述是小编给大家介绍的Java中Properties的使用详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的网站模板支持!
相关文章
红米Note4电信4G手机评测(体验超高速网络,畅享流畅生活)
摘要:近年来,随着4G网络的普及和提速,手机成为人们日常生活中不可或缺的工具。而作为手机市场中的佼佼者,红米Note4电信4G手机以其出色的性能和稳定的网络连接,赢得了众多用户的青睐。本...2025-11-05- 复制C:ProgramFilesMicrosoftSQLServerMSSQL.1MSSQLDATAAdventureWorks_Data.mdf 1.2025-11-05
近日,微软对PowerShell 7中的一个.NET内核远程代码执行漏洞发布了预警信息,漏洞产生的原因是.NET 5和.NET内核中的文本编码方式引发的。PowerShell 是运行在windows机2025-11-05速速更新你的iPhone,新漏洞大得可怕,全球16.5亿台设备受影响
复制PDFComment%PDF-1.3\n\n obj10 Type:/XRef Referencing: C2025-11-05- 摘要:随着电子产品的普及和更新换代速度的加快,电子废弃物也日益增多。华为作为一家国际知名的通信技术领先企业,不仅致力于科技创新,更积极推动环保回收,倡导绿色循环利用,致力于构建可持续发展...2025-11-05
- 复制select*fromtable1with(updlock)where 1.2025-11-05

最新评论