JAVA读取配置文件可以使用Properties类,非常方便。
我把上一篇的代码做了修改,下面的 Configration 类是用来读取配置文件连接数据库。
Configration.java:
我把上一篇的代码做了修改,下面的 Configration 类是用来读取配置文件连接数据库。
Configration.java:
package
src.dbtest;
import java.io.FileInputStream;
import java.util.Properties;
public class Configration {
private Properties properties ;
private FileInputStream fileInputStream ;
// private FileOutputStream fileOutputStream ;
private String value;
/**
* @param
*/
public Configration() {
properties = new Properties();
}
/**
* @param filePath : Path+Name of config file
*/
public Configration(String filePath) {
this();
try {
fileInputStream = new FileInputStream(filePath);
properties.load(fileInputStream);
fileInputStream.close();
} catch (Exception e) {
System.out.println("Read file failed! Please check the filepath.");
e.printStackTrace();
}
}
/**
* @param String key
* @return String value
*/
public String getValue(String key) {
if (properties.containsKey(key)) {
import java.io.FileInputStream;
import java.util.Properties;
public class Configration {
private Properties properties ;
private FileInputStream fileInputStream ;
// private FileOutputStream fileOutputStream ;
private String value;
/**
* @param
*/
public Configration() {
properties = new Properties();
}
/**
* @param filePath : Path+Name of config file
*/
public Configration(String filePath) {
this();
try {
fileInputStream = new FileInputStream(filePath);
properties.load(fileInputStream);
fileInputStream.close();
} catch (Exception e) {
System.out.println("Read file failed! Please check the filepath.");
e.printStackTrace();
}
}
/**
* @param String key
* @return String value
*/
public String getValue(String key) {
if (properties.containsKey(key)) {