Topic: 有关properties文件

  Print this page

1.有关properties文件 Copy to clipboard
Posted by: bbbaby
Posted on: 2003-08-26 20:16

如何在程序中读取譬如db.properties文件呢~?
我用FileInputStream总是说找不到文件,
properties这类文件用什么取比较好呢~?(我想做数据库连接池)
先谢谢各位大虾拉~~^_^

2.Re:有关properties文件 [Re: bbbaby] Copy to clipboard
Posted by: collins
Posted on: 2003-08-26 20:24

java.util.properties

3.Re:有关properties文件 [Re: bbbaby] Copy to clipboard
Posted by: bbbaby
Posted on: 2003-08-26 20:32

不是这个意思,是怎么读取本地文件db.properties啊~?

4.Re:有关properties文件 [Re: bbbaby] Copy to clipboard
Posted by: bbbaby
Posted on: 2003-08-26 20:35

我会了,^_^

5.Re:有关properties文件 [Re: bbbaby] Copy to clipboard
Posted by: bbbaby
Posted on: 2003-08-26 21:33

为什么我这么取程序说取不到文件?

import java.io.*;
import java.util.*;

public class GetDB{
public static void main(String[] args) throws FileNotFoundException, IOException{
    Properties prop = new Properties();
    prop.load(new FileInputStream("DB.properties"));
    String ClassName = prop.getProperty("DRIVER_CLASSNAME");
    System.out.println(ClassName);
  }
}

还是取properties文件时一定要在方法中取??

6.Re:有关properties文件 [Re: bbbaby] Copy to clipboard
Posted by: ftang
Posted on: 2003-08-26 23:36

[得分不因为以下的程式码特别出色,而是印象中大侠多次热心助人:-) --why]

properties 文件可以有jre指定,看看如何指定java的system properties, 就会知道,这里给一个很小巧的propertiesLoader class,基本可以满足所有你需要的。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.*;

public class PropertiesLoader{

/**
* member variables.
**/
private static final String _PROPERTYFILE = "accesspoint.properties";
private static Logger errLog = Logger.getLogger("PropertiesLoader");
private static Properties props;

/**
* private constructor to prevent outside to call.
**/
private PropertiesLoader(){}

/**
* Depricauted
**/
public static String getProperties(){return null;}

/**
* This method gonna return the full file path you define in the accesspoint.properties file.
*
* @param String propertie name
* @return String the propertie file full path you define in the accesspoint.properties file.
**/
public static String getPropertie(String name){
    if(props == null){
      reload();
    }
String rtnValue = null;
rtnValue = props.getProperty(name);
errLog.info("PropertiesLoader::return value: " + rtnValue + " for name: " + name);
return rtnValue;
}

/**
* this method will be called to load all the properties in accesspoint.peropertie file into the propertie object.
* this method only be called when the first user call this class.
**/
public static void reload(){
FileInputStream file = null;

// current working directory
  String workingDir = System.getProperties().getProperty("user.dir");
  String fullPath = (workingDir == null) ? (".\\" + _PROPERTYFILE) : (workingDir + "\\" + _PROPERTYFILE);
  errLog.info("Properties file path is: " + fullPath);

try{
file = new FileInputStream(_PROPERTYFILE);
props = new Properties();
props.load(file);
}catch( FileNotFoundException fe){
   errLog.error("PropertiesLoader::Cann't find the properties file:", fe);
  }catch( IOException ioe){
errLog.error("PropertiesLoader::IO Exception:", ioe);
}catch(Exception e){
errLog.error("Generic Exception Caught: ", e);
}
}

}

有什么问题可以讨论

7.Re:有关properties文件 [Re: bbbaby] Copy to clipboard
Posted by: floater
Posted on: 2003-08-27 01:20

bbbaby wrote:
为什么我这么取程序说取不到文件?

import java.io.*;
import java.util.*;

public class GetDB{
public static void main(String[] args) throws FileNotFoundException, IOException{
    Properties prop = new Properties();
    prop.load(new FileInputStream("DB.properties"));
    String ClassName = prop.getProperty("DRIVER_CLASSNAME");
    System.out.println(ClassName);
  }
}

还是取properties文件时一定要在方法中取??

do a print on "new FileInputStream("DB.properties"))", I bet it's null. This file should be under the project root. If you don't where, put it everywhere, then you know where.

8.Re:有关properties文件 [Re: bbbaby] Copy to clipboard
Posted by: bbbaby
Posted on: 2003-08-27 08:38

thank you very much, i will try it~!

9.Re:有关properties文件 [Re: bbbaby] Copy to clipboard
Posted by: bbbaby
Posted on: 2003-08-27 08:41

我写了全路经,现在能取到了~~谢谢各位

10.Re:有关properties文件 [Re: bbbaby] Copy to clipboard
Posted by: ftang
Posted on: 2003-08-27 11:47

用全路径不是一个真正的solution, 正确的做法应该是使用 -D<name>=<value>set a system property 来调用环境变量或者叫常量,

如果是在j2ee中,许多application server 都允许设置各种JVM的参数包括内存使用大小,gabage collection report, system property,运用得当的话,监控JVM绝对不是难事,别以为ibm或weblogic的system tools有什么了不起,他们也不过调用了JVM设置和reflect API, 读读JBOSS的源码,就可以看到,

另,给出的source code中,他会自动报出当前运行的目录。

11.Re:有关properties文件 [Re: bbbaby] Copy to clipboard
Posted by: bbbaby
Posted on: 2003-08-27 12:50

多谢提醒,我还没有看过jboss的源码呢,等会去下个看看,提高一下,^_^


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923