加载类
1.文件中加载:
new FileInputStream("src_out.properties")
1.静态方法:
ClassLoader.getSystemResourceAsStream("src_in.properties")
JavaSE阶段
2.非静态方法
loader.getResourceAsStream("src_in.properties")
Web阶段
4. config
package projict06; import java.io.FileInputStream; /* 加载类 1.文件中加载: new FileInputStream("src_out.properties") 1.静态方法: ClassLoader.getSystemResourceAsStream("src_in.properties") JavaSE阶段 2.非静态方法 loader.getResourceAsStream("src_in.properties") Web阶段 4. config */ import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import org.junit.Test; public class test_properties { @Test public void test05() throws Exception { //1.创建一个properties对象,用来加载数据(key=value) Properties pro = new Properties(); //2.config是一个Source Folder ClassLoader loader = test_properties.class.getClassLoader(); pro.load(loader.getResourceAsStream("other.properties")); //3.遍历显示 System.out.println(pro); } @Test public void test04() throws Exception { //1.创建一个properties对象,用来加载数据(key=value) Properties pro = new Properties(); //2.从文件src_in.properties文件中加载数据--非静态方法 //先获取当前类的加载器对象 ClassLoader loader = test_properties.class.getClassLoader(); pro.load(loader.getResourceAsStream("projict06/src.properties")); //3.遍历显示 System.out.println(pro); } @Test public void test03() throws Exception { //1.创建一个properties对象,用来加载数据(key=value) Properties pro = new Properties(); //2.从文件src_in.properties文件中加载数据--非静态方法 //先获取当前类的加载器对象 ClassLoader loader = test_properties.class.getClassLoader(); pro.load(loader.getResourceAsStream("src_in.properties")); //3.遍历显示 System.out.println(pro); } @Test public void test02() throws Exception { //1.创建一个properties对象,用来加载数据(key=value) Properties pro = new Properties(); //2.从文件src_in.properties文件中加载数据--静态方法 pro.load(ClassLoader.getSystemResourceAsStream("src_in.properties")); //3.遍历显示 System.out.println(pro); } @Test public void test01() throws Exception { //1.创建一个properties对象,用来加载数据(key=value) Properties pro = new Properties(); //2.从文件src_out.properties文件中加载数据 pro.load(new FileInputStream("src_out.properties")); //3.遍历显示 System.out.println(pro); } }
原文:https://www.cnblogs.com/hapyygril/p/13026843.html