58,452
社区成员
发帖
与我相关
我的任务
分享
package org.programming.student;
import java.util.*;
import java.io.*;
/**
*该类用来读取配置文件中的属性,并在设计该类时使用了单例模式。
*
*/
public class ConfigReader
{
private static ConfigReader instance = new ConfigReader();
private Properties pros = new Properties();
private ConfigReader()
{
//该处使属性集和指定文件进行关联
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/programming/student/students.properties");
/* InputStream in = ConfigReader.class.getResourceAsStream("students.properties"); //也可以使用这种方式 */
try{
this.pros.load(in);
}catch(IOException e){
e.printStackTrace();
}
}
public static ConfigReader getInstance()
{
return instance;
}
public String getPropertyValue(String key)
{
return this.pros.getProperty(key);
}
public static void main(String[] args)
{
String driverName = getInstance().getPropertyValue("driver-name");
String url = getInstance().getPropertyValue("url");
System.out.println(url);
}
}
package org.programming.student;
import java.util.*;
import java.io.*;
/**
*该类用来读取配置文件中的属性,并在设计该类时使用了单例模式。
*
*/
public class ConfigReader
{
private static ConfigReader instance = new ConfigReader();
private Properties pros = new Properties();
private ConfigReader()
{
//该处使属性集和指定文件进行关联
InputStream in =
Thread.currentThread().getContextClassLoader().getResourceAsStream("students.properties");
try{
this.pros.load(in);
}catch(IOException e){
e.printStackTrace();
}
}
public static ConfigReader getInstance()
{
return instance;
}
public String getPropertyValue(String key)
{
return this.pros.getProperty(key);
}
public static void main(String[] args)
{
String driverName = getInstance().getPropertyValue("driver-name");
String url = getInstance().getPropertyValue("url");
System.out.println(url);
}
}
这是源码,麻烦您了!