一、要点
1.通用方式
2.流的形式
package com.xit.reflect;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class IoPropertiesTest { public static void main(String[] args) throws IOException {// String path = Thread.currentThread().getContextClassLoader().getResource("classinfo2.properties").getPath();// FileReader reader = new FileReader(path); /* 流的形式 */ InputStream reader = Thread.currentThread().getContextClassLoader(). getResourceAsStream("classinfo2.properties"); Properties pro = new Properties(); pro.load(reader); reader.close(); String className = pro.getProperty("className"); System.out.println(className); }}
二、ResourceBundle替代IO(最终)
1、IO + Properties,怎么快速绑定属性资源文件?
//要求:第一这个文件必须在类路径下<br /> //第二这个文件必须是以.properties结尾。<br /> ResourceBundle bundle = ResourceBundle.getBundle("com/bjpowernode/test");<br /> String value = bundle.getString(key);
package com.xit.reflect;import java.util.ResourceBundle;public class ResourceBundleTest { public static void main(String[] args) { //资源绑定器,只能绑定xxx.properties文件。并且这个文件必须在类路径下,文件扩展名必须是properties //并且在写路径的时候,路径后面的扩展名不能写 ResourceBundle bundle = ResourceBundle.getBundle("classinfo2"); String className= bundle.getString("className"); System.out.println(className); }}