在环境变量中设置一个环境变量CONFIG_FILE,值就是你的配置文件的路径。

解决方案 »

  1.   

    InputStream inputStream =new FileInputStream("server.properties");//文件路径        Properties property = new Properties();            property.load(inputStream);
      

  2.   

    我想请问一下:CONFIG_FILE 文件应该放在哪里,java Test通过后就结贴
    //CONFIG_FILE怎么能是文件呢!?
    getProperties() 
    确定当前系统属性getProperty(String, String) 
    获取指定关键字指示的系统属性。//建议你看一下SYSTEM类的介绍~~~~
      

  3.   

    取之前,要先设置CONFIG_FILE的属性值,这可以通过java - 命令行来完成(在命令行输入java回车后,会有帮助信息提示如何完成),我的jdk有些问题^.^,现在只好在代码设下啦   System.getProperties().setProperty("CONFIG_FILE","c:/boot.properties");
       
        cfgFileName = System.getProperties().getProperty("CONFIG_FILE");
        
       //test here : c:/boot.properties
        System.out.println("sys prop " + System.getProperties());
      

  4.   

    所以具体放在哪,是由设置CONFIG_FILE的属性值决定的如(c:/boot.properties)
      

  5.   

    cuizm(射天狼)是对的,CONFIG_FILE根本就不是个文件,而且也不是System的属性.
    class Test{
    public static void main(String args[])throws Exception {
            String cfgFileName = null;
    cfgFileName = System.getProperties().getProperty("CONFIG_FILE");
    System.out.println(cfgFileName);//打印null
        }
    }
      

  6.   

    easy
    java -DCONFIG_FILE=xxxx Test
      

  7.   

    谢谢大家了,有这么多热心的人。java -DCONFIG_FILE=xxxx Test
    运行通过了。
      

  8.   

    import java.io.FileInputStream;
    import java.util.Properties;public class Test
    {
    private static String dsName = null;
    private static String username = null;
    private static String password = null;
        
    public static void main(String args[])
    throws Exception
    {
    String cfgFileName = null;
    try
    {
                        cfgFileName = System.getProperties().getProperty("CONFIG_FILE");
    System.out.println("cfgFileName=" + cfgFileName);
    //cfgFileName=null!!!
    // set up new properties object
     // from file "myProperties.txt" 
     //this file inclue text: 
     //system.datasource.name=abc  
    //ystem.database.username=abc  
    //system.database.password=abc  

    FileInputStream propFile = new FileInputStream("D:/myProperties.txt");
    Properties p = new Properties(System.getProperties());
    p.load(propFile);
    //set the system properties
    System.setProperties(p);

    dsName = p.getProperty("system.datasource.name");
    username = p.getProperty("system.database.username");
    password = p.getProperty("system.database.password");
    propFile.close(); //display new properties
    System.getProperties().list(System.out);

    }
    catch(Exception ex)
    {
    throw new Exception(String.valueOf(String.valueOf((new StringBuffer("Load from configfile(")).append(cfgFileName).append(") error . ").append(ex.getMessage()))));
    }
    }
    }