import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;
/**
*
* <b>Application name:</b><br>
* <b>Application describing:</b> <br>
*/
public class PropertiesUtil
{
/**
*
* {读取项目中的properties配置文件}<br>
* 返回的是ResourceBundle的对象
*
* @param propertiesPath
* @return
*/
public static ResourceBundle readFromProject(String propertiesFilePath)
{
return ResourceBundle.getBundle(propertiesFilePath);
}
/**
*
* {读取磁盘上的properties配置文件}<br>
* 返回的市Properties的对象
* @param propertiesFilePath
* @return
*/
public static Properties readFromSystemFile(String propertiesFilePath)
{
InputStream is = null;
Properties properties = null;
try
{
is = new FileInputStream(propertiesFilePath);
properties = new Properties();
properties.load(is);
}
catch (FileNotFoundException e)
{
System.out.println("没有找到文件");
e.printStackTrace();
}
catch (IOException e)
{
System.out.println("读取properties文件失败");
e.printStackTrace();
}
finally
{
if (is != null)
{
try
{
is.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return properties;
}
/**
*
* {主函数}
*
* @param args
*/
public static void main(String[] args)
{
//ResourceBundle resourceBundle = readFromProject("db1");
ResourceBundle resourceBundle = readFromProject("com.neusoft.config.db");
String username = resourceBundle.getString("username");
String password = resourceBundle.getString("password");
String url = resourceBundle.getString("url");
String driverClassName = resourceBundle.getString("driverClassName");
System.out.println(username);
System.out.println(password);
System.out.println(url);
System.out.println(driverClassName);
String filePath = "c:" + File.separator + "test" + File.separator + "config.properties";
Properties properties = readFromSystemFile(filePath);
String base = properties.getProperty("base");
String step = properties.getProperty("step");
System.out.println(base + " " + step);
}
}