`

可以和流相关联的集合对象Properties

    博客分类:
  • io
 
阅读更多

最近学习流的时候看到了这个集合类可以和流一起想关联操作:

     先上代码

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Properties {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		newProperties();
		loadProperties();
		
	}
	
	//生成新的properties文件
	
	static void newProperties() {
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter("test.properties"));
			java.util.Properties p = new java.util.Properties();
			p.setProperty("美女1", "张馨予");
			p.setProperty("美女2", "周韦彤");
			p.store(bw, "Do you want fuck?");
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/*test.properties:
	 * 
			#Do you want fuck?
			#Thu Feb 09 10:33:03 CST 2012
			美女2=周韦彤
			美女1=张馨予
	*/
	
	//加载properties文件
	static void loadProperties(){
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader("test.properties"));
			java.util.Properties p = new java.util.Properties();
			p.load(br);
			p.list(System.out);
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/*result:
		-- listing properties --
		美女2=周韦彤
		美女1=张馨予*/
		
	
	
	

}

 

上面这段代码可以在当前目录下生成一个键值对的文本文件并读取这个文件,写入文件用的是store方法,该方法支持OutputStream和Reader参数的重载,第二个参数是注释。读取文件呢用的是load方法,该方法支持InputStream和Writer参数的重载。在写入和读取文件的时候都要强制捕获可能会抛出的一个IOException。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class XmlProperties {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		newXmlProperties();
		loadXmlProperties();
	}

	//使用properties类创建一个新的xml
	static void newXmlProperties() throws IOException{
		Properties p = new Properties();
		p.setProperty("大mimi", "干露露");
		p.setProperty("长腿腿", "艾尚真");
		BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream("test.xml"));
		p.storeToXML(os, "sex girl");
		os.close();
	}
	/*test.xml:
	 * 
		<?xml version="1.0" encoding="UTF-8" standalone="no"?>
		<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
		<properties>
		<comment>sex girl</comment>
		<entry key="大mimi">干露露</entry>
		<entry key="长腿腿">艾尚真</entry>
		</properties>*/
	
	//读取properties创建的xml文件
	static void loadXmlProperties() throws InvalidPropertiesFormatException, IOException{
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test.xml"));
		Properties p = new Properties();
		p.loadFromXML(bis);
		bis.close();
		p.list(System.out);
	}
	
	/*result:
		-- listing properties --
		大mimi=干露露
		长腿腿=艾尚真*/
}

 

上面的代码是用properties类生成一个特定节点的xml文件和读取它,可以看的出来这个xml文件的格式是properties特定的,也就是写死的,一般应该不会用他读写xml,因为太不灵活了,写入xml是用storeXml方法,只能接受OutputStream的参数而不能接受Writer类的实例做参数,第二个参数是注释,第三个是使用的编码格式(可选)。也会抛出一个IOException。读取xml文件也是只能用InputStream做参数,抛出的异常是InvalidPropertiesFormatException,是propertie特定的异常。

 

练习:记录一个程序运行的次数,当满足指定时该就不可以再继续了。 练习:记录一个程序运行的次数,当满足指定时该就不可以再继续了。

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class Test {
	class Program implements Runnable{
		int i = 0;
		public void run() {
			// TODO Auto-generated method stub
			System.out.println((++i)+" Run.... ");
		}
		
	}
	
	static int addCount(File file) throws IOException{
		int i = getRunCount(file);
		Properties p = new Properties();
		p.setProperty("runCount", String.valueOf(++i));
		BufferedWriter wr = new BufferedWriter( new FileWriter(file));
		p.store(wr,"runCount");
		wr.flush();
		wr.close();
		return i;
	}
	
	static int getRunCount(File file) throws IOException{
		if(file.exists()){
			Properties p = new Properties();
			FileInputStream bis = new FileInputStream(file);
			p.load(bis);
			String c = p.getProperty("runCount");
			bis.close();
			if(c == null){
				return 0;
			}
			return Integer.parseInt(c);
		}
		return 0;
		
	}
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Test.Program program = new Test().new Program();
		File f = new File("runCount.properties");
		int i = 0;
		while(i < 10){
			program.run();
			i = addCount(f);
		}
	}
}

 java读取properties文件的六种方法

使用j2se api读取properties文件的六种方法

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)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics