怎么读取配置文件app.properties中的信息

xiaozao617 2012-01-06 08:58:32
应用程序配置文件app.properties中包含如下一些信息:
name=administor
pwd=554499
port=8000
怎么获取这个文件中的值呢?(最好使用Singleton的方式,有其它方式也行)
哪位兄台知道的话,麻烦进来指点指点!!!
...全文
509 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
林林林朝夕 2012-02-06
  • 打赏
  • 举报
回复
相对路径的写法:

InputStream in = new FileInputStream(new File("../../app.properties"));

../代表此类的上一级目录。


zuxianghuang 2012-02-06
  • 打赏
  • 举报
回复
public final static String APP_CONFIG_FILENAME = "app.properties";

public final static String NAME= "name";

public final static String PWD = "pwd";

public final static String PORT = "port";

public static void init()
{
InputStream inputStream = ClassLoader.getSystemResourceAsStream(APP_CONFIG_FILENAME);
Properties properties = new Properties();
try
{
properties.load(inputStream);
}
catch (IOException e)
{
e.printStackTrace();
}
String name= properties.getProperty(NAME);
String pwd= properties.getProperty(PWD);
String port= properties.getProperty(PORT);
}
qq931226110 2012-02-06
  • 打赏
  • 举报
回复
Properties props = new Properties();
InputStream in = new FileInputStream(new File("app.properties"));
props.load(in);
in.close();
System.out.println("name = "+props.get("name"));

xiaozao617 2012-01-07
  • 打赏
  • 举报
回复
不知道相对路径怎么写的,我只好把在哪个盘的绝对路径写出来了,这样总算读取出来了!
xiaozao617 2012-01-07
  • 打赏
  • 举报
回复
原来是这么写的,谢了! 不过我把app.properties放在src目录下,怎么读取的时候老是报“系统找不到指定的路径”
xieshengjun2009 2012-01-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 canghaiguzhou 的回复:]

Java code



Properties props = new Properties();
InputStream in = new FileInputStream(new File("app.properties"));
props.load(in);
in.close();
System.out.println("name = "+props……
[/Quote]+1
zx8813443 2012-01-06
  • 打赏
  • 举报
回复



Properties props = new Properties();
InputStream in = new FileInputStream(new File("app.properties"));
props.load(in);
in.close();
System.out.println("name = "+props.get("name"));

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧