直接上代码:

package test.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties; public class TestProperties { public static void main(String[] args) throws FileNotFoundException,
IOException, Exception {
Properties pro = init.getPro();
String apple = pro.getProperty("apple");
System.out.println(apple); // list()方法:该方法多用于调试,开发中很少用
pro.list(System.out); // test2: store()方法生成持久化文件
// init.proStore(); } } // 操作属性文件
class init { public static Properties proList() {
Properties pro = new Properties();
pro.setProperty("11", "one");
pro.setProperty("22", "two");
pro.setProperty("33", "throw"); pro.list(System.out);// 该方法多用于调式,开发中很少用 return pro;
} public static void proLoad() throws Exception {
File f = new File("fruit.properties");
FileInputStream is = new FileInputStream(f); Properties pro = new Properties();
pro.load(is); pro.list(System.out);
} public static void proStore() throws Exception { Properties pro = proList(); FileOutputStream fos = new FileOutputStream("fruit1.properties"); pro.store(fos, "TEST"); fos.close();
} public static Properties getPro() throws FileNotFoundException, IOException { Properties pro = new Properties(); // 在根目录下
File f = new File("fruit.properties");
if (f.exists()) {
pro.load(new FileInputStream(f));
} else {
pro.setProperty("apple", "Reflect.Apple");
pro.setProperty("one", "Reflect.One"); // 想要将这个集合中的字符串键值信息持久化存储到文件中,需要关联输出流
// store()方法是持久化,没有这个方法,则不会生成fruit.properties文件
pro.store(new FileOutputStream(f), "FRUIT CLASS");
} return pro;
}
}

api:

load()和store()

void load(InputStream inStream)

Reads a property list (key and element pairs) from the input byte stream.
void store(OutputStream out, String comments)
Writes this property list (key and element pairs) in this Properties table to the output stream in a format suitable for loading into a Properties table using the

这是一对。

以下是另一对:

load(Reader reader)
Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format.
store(Writer writer, String comments)
Writes this property list (key and element pairs) in this Properties table to the output character stream in a format suitable for using the load(Reader) method.

附1:

1.Properties类与Properties配置文件

  Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。

2.Properties中的主要方法

(1)load(InputStream inStream)

   这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象如下面的代码:

Properties pro = new Properties();
FileInputStream in = new FileInputStream("a.properties");
pro.load(in);
in.close();

(2)store(OutputStream out, String comments)

  这个方法将Properties类对象的属性列表保存到输出流中如下面的代码:

FileOutputStream oFile = new FileOutputStream(file, "a.properties");
pro.store(oFile, "Comment");
oFile.close();

  如果comments不为空,保存后的属性文件第一行会是#comments,表示注释信息;如果为空则没有注释信息。

  注释信息后面是属性文件的当前保存时间信息。

(3)getProperty/setProperty

  这两个方法是分别是获取和设置属性信息。

附2:一些网上的资料:Java 加载Properties文件的六种方式:http://blog.csdn.net/yz394777014/article/details/4330583

1。使用java.util.Properties类的load()方法
示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);

2。使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

3。使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);

4。使用class变量的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);

补充

Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);

Properties类一些常用的用法的更多相关文章

  1. 关于Properties类常用的操作

    import java.io.*;import java.util.Enumeration;import java.util.Properties;/** * 关于Properties类常用的操作 * ...

  2. Java常用类之Properties类

    1.特性 Properties类表示了一个持久的属性集,可保存在流中或从流中加载,实现内存和文件的交互.Properties继承了Hashtable<Object,Object>类,可以使 ...

  3. File类与常用IO流第七章——Properties集合

    Properties概述 java.util.Properties extends Hashtable<k,v> implements Map<k,v> Properties类 ...

  4. Properties类随笔

    1. 体系介绍 Properties类继承自HashTable,勉强属于集合框架体系一员,键值对形式存储数据,当然键肯定是唯一的,底层哈希表保证键的唯一,此类一般用于表示配置文件. 2. 基本用法 由 ...

  5. Java 数据类型:集合接口Map:HashTable;HashMap;IdentityHashMap;LinkedHashMap;Properties类读取配置文件;SortedMap接口和TreeMap实现类:【线程安全的ConcurrentHashMap】

    Map集合java.util.Map Map用于保存具有映射关系的数据,因此Map集合里保存着两个值,一个是用于保存Map里的key,另外一组值用于保存Map里的value.key和value都可以是 ...

  6. CSS中伪类及伪元素用法详解

    CSS中伪类及伪元素用法详解   伪类的分类及作用: 注:该表引自W3School教程 伪元素的分类及作用: 接下来让博主通过一些生动的实例(之前的作业或小作品)来说明几种常用伪类的用法和效果,其他的 ...

  7. 【JAVA Properties类概述】

    一.概述. 之前说过,该对象是和IO流相结合的技术,所以和IO流结合在一起来讲比较合适. public class Propertiesextends Hashtable<Object,Obje ...

  8. Java中Properties类的操作

    知识学而不用,就等于没用,到真正用到的时候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用Java来写,外加 ...

  9. Java中Properties类的操作配置文件

    知识学而不用,就等于没用,到真正用到的时 候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用 Java来写, ...

随机推荐

  1. 从零开始设计SOA框架(三):请求参数的加密方式

    第二章中说明请求参数有哪些,主要是公共参数和业务参数,服务端需要对参数进行效验,已验证请求参数的合法性 参数效验前先解释下以下参数: 1.参数键值对:包括公共参数.业务参数      1.公共参数:按 ...

  2. Html-input文本框只能输入数字

    onKeyPress="if ((event.keyCode < 48 || event.keyCode > 57)) event.returnValue = false;&qu ...

  3. Java基础-静态代理与动态代理比较

    JAVA的静态代理与动态代理比较 静态代理类: 由程序员创建或由特定工具自动生成源代码,再对其编译.在程序运行前,代理类的.class文件就已经存在了.动态代理类: 在程序运行时,运用反射机制动态创建 ...

  4. Search everything 使用说明

    Everything是速度最快的文件搜索软件,可以瞬间搜索到你需要的文件.

  5. C++ Standard Template Library STL(undone)

    目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...

  6. c链表实现遇到的错误

    想完成一个链表发现有错误,代码如下: //http://ac.jobdu.com/problem.php?pid=1511 //֮ǰÓÃlistʵÏֵ쬽ñÌìÊÔÒ»ÏÂÓÃstruct ...

  7. Redis操作+python

    自动化接口测试中需要向redis中插入测试数据: 1. 连接redis: import redisself.r = redis.StrictRedis(host=env.REDIS_HOST, por ...

  8. auto,register,static实例

    #include <stdio.h>int main() {    auto int i = 0;    register int j = 0;    static int k = 0;  ...

  9. another app is currently holding the yum lock;waiting for it to exit解决

    有时用yum升级一些文件时,会出现以下情况:   another app is currently holding the yum lock;waiting for it to exit...   可 ...

  10. 如何用jar命令对java工程进行打包

    如何用jar命令对java工程进行打包 有时候为了更方便快捷的部署和执行Java程序,要把java应用程序打包成一个jar包.而这个基础的操作有时候也很麻烦,为了方便java程序员们能够方便的打包ja ...