package com.cttsp.test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesRead {
/****
* 读取根目下web-inf/config目录下的jdbc.properties文件
*/
private static String address="";
/***
* 从属性文件中读取配置的属性,从类第一次加载时就读取,只读一次
*/
static{
Properties prop = new Properties();
String path =PropertiesRead.class.getResource("/").getPath();
System.out.println(path);
//获取web-inf目录的路径
path=path.substring(1, path.indexOf("build"));
System.out.println(path);
InputStream in=null;
try {
//读去文件
//E:\workspaseHY\HYTT\WebContent\WEB-INF\config
in =new FileInputStream(path+"WebContent/WEB-INF/config/jdbc.properties");
prop.load(in);
address = prop.getProperty("driverClassName");
System.out.println(address);
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(in!=null) in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesRead {
/****
* 读取根目下web-inf/config目录下的jdbc.properties文件
*/
private static String address="";
/***
* 从属性文件中读取配置的属性,从类第一次加载时就读取,只读一次
*/
static{
Properties prop = new Properties();
String path =PropertiesRead.class.getResource("/").getPath();
System.out.println(path);
//获取web-inf目录的路径
path=path.substring(1, path.indexOf("build"));
System.out.println(path);
InputStream in=null;
try {
//读去文件
//E:\workspaseHY\HYTT\WebContent\WEB-INF\config
in =new FileInputStream(path+"WebContent/WEB-INF/config/jdbc.properties");
prop.load(in);
address = prop.getProperty("driverClassName");
System.out.println(address);
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(in!=null) in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}