主要是给自己留个印象,忘了可以再来翻翻。当然这个东西也是借鉴了别人的,
package com.fd.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
public class LoadProperties {
private static String Path_properties = "src/com/fd/util/config.properties";//全类名
private static String Url = null;
private static String DriverClassName = null;
private static String User = null;
private static String Password = null;
**第一种:**
/**
* 全路径的文件,全类名是某个文件在项目中的位置,格式为包名.类名
*/
public static void loadProperties1(){
//获得properties
Properties property = new Properties();//流文件
InputStream ins = null;
try {
ins = new FileInputStream(Path_properties);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
property.load(ins);
} catch (IOException e1) {
System.out.println("Properties加载失败");
e1.printStackTrace();
}
}
**第二种:**
/**
* 包路径,根路径前要加"/"
*/
public static void loadProperties2(){
InputStream ins = LoadProperties.class.getResourceAsStream("/com/fd/util/config.properties");
Properties property = new Properties();
try {
property.load(ins);
} catch (IOException e) {
e.printStackTrace();
}
printProperties(property);
}
**第三种:**
/**
* 包路径+properties文件名+.后缀
*/
public static void loadProperties3(){
InputStream ins = LoadProperties.class.getClassLoader().getResourceAsStream("com/fd/util/config.properties");
Properties property = new Properties();
try {
property.load(ins);
} catch (IOException e) {
e.printStackTrace();
}
printProperties(property);
}
**第四种:**
/**
* 包路径+properties文件名+.后缀
*/
public static void loadProperties4(){
InputStream ins = ClassLoader.getSystemResourceAsStream("com/fd/util/config.properties");
Properties property = new Properties();
try {
property.load(ins);
} catch (IOException e) {
e.printStackTrace();
}
}
第五种:
/**
* 包路径+properties文件名
*/
public static void loadProperties5(){
ResourceBundle rb = ResourceBundle.getBundle(“com/fd/util/config”);
//rb.getString()方法,参数是properties文件的各属性名
System.out.println(rb.getString("mysql.driver"));
System.out.println(rb.getString("mysql.url"));
System.out.println(rb.getString("mysql.user"));
System.out.println(rb.getString("mysql.password"));
}
**第六种:**
/**
* properties的全类名 在项目中成功
* 使用java.util.PropertyResourceBundle类的构造函数
*/
public void loadProperties6(){
InputStream ins = null;
ResourceBundle rb = null;
try {
ins = new BufferedInputStream(new FileInputStream(Path_properties));
rb = new PropertyResourceBundle(ins);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//rb.getString()方法,参数是properties文件的各属性名
System.out.println(rb.getString("jdbc.driverClassName"));
System.out.println(rb.getString("jdbc.url"));
System.out.println(rb.getString("jdbc.username"));
System.out.println(rb.getString("jdbc.password"));
}
/**方法 输出 Properties 的属性
* @param property
*/
public static void printProperties(Properties property){
//连接属性
DriverClassName = property.getProperty("mysql.driver");
Url = property.getProperty("mysql.url");
User = property.getProperty("mysql.user");
Password = property.getProperty("mysql.password");
System.out.println(DriverClassName+"\n"+Url+"\n"+User+"\n"+Password);
System.out.println();
}
public static void main(String[] args) {
// loadProperties1();
// loadProperties2();
// loadProperties3();
// loadProperties4();
loadProperties5();
}
}