在环境变量中设置一个环境变量CONFIG_FILE,值就是你的配置文件的路径。
解决方案 »
- 如何使JComboBox在显示时就展开下拉列表?
- java 中有没有内存流,可以先写入数据,再读取数据
- 谁能综合的介绍一下Container 如set,List,map及其实现类
- 怎么将类似\u5173\u4e8e的东西转化为汉字?
- 郁闷!rs.first();就提示“对只转发结果集的无效操作: first”
- 关于输入输出流的问题,救命啊。。。。
- 关于socket通信问题
- 一个Class调用另外一个class的问题
- 那里有JDK1.4down???我要用里面的那个javax.crypto类...
- Java byte转中文乱码
- .java编译后显示错误信息package javax.servlet does not exist该这么解决
- 关于货币大小写转换的问题
//CONFIG_FILE怎么能是文件呢!?
getProperties()
确定当前系统属性getProperty(String, String)
获取指定关键字指示的系统属性。//建议你看一下SYSTEM类的介绍~~~~
cfgFileName = System.getProperties().getProperty("CONFIG_FILE");
//test here : c:/boot.properties
System.out.println("sys prop " + System.getProperties());
class Test{
public static void main(String args[])throws Exception {
String cfgFileName = null;
cfgFileName = System.getProperties().getProperty("CONFIG_FILE");
System.out.println(cfgFileName);//打印null
}
}
java -DCONFIG_FILE=xxxx Test
运行通过了。
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()))));
}
}
}