Properties类一些常用的用法

时间:2021-06-23 16:34:36

直接上代码:

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);