代码
package day19;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.junit.Test;
public class Main {
/**
* 测试类加载器;
* @throws IOException
*/
@Test
public void method2() throws IOException {
//方法一:适用用当配置文件位于包下;
ClassLoader classLoader = this.getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("day19\\jdbc.properties");
Properties properties = new Properties();
properties.load(inputStream);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
System.out.println("----------------------第一种方法------------------------");
System.out.println(username);
System.out.println(password);
//方法二:适用于配置文件位于整个工程下面;
FileInputStream fis = new FileInputStream(new File("jdbc.properties"));
Properties properties1 = new Properties();
properties1.load(fis);
String username1 = properties1.getProperty("username");
String password1 = properties1.getProperty("password");
System.out.println("----------------------第二种方法------------------------");
System.out.println(username1);
System.out.println(password1);
}
}
结果