我正在處理一個項目,其中需要從config.properties
文件中讀取所有項目。下面是我config.properties
文件 -如果在命令行參數中指定了覆蓋屬性文件的值
NUMBER_OF_THREADS: 100
NUMBER_OF_TASKS: 10000
ID_START_RANGE: 1
TABLES: TABLE1,TABLE2
,我來自像這個 - 命令提示符下運行了一個程序,它工作正常。
java -jar Test.jar "C:\\test\\config.properties"
下面是我的程序 -
private static Properties prop = new Properties();
private static int noOfThreads;
private static int noOfTasks;
private static int startRange;
private static String location;
private static List<String> tableNames = new ArrayList<String>();
public static void main(String[] args) {
location = args[0];
try {
readPropertyFiles();
} catch (Exception e) {
LOG.error("Threw a Exception in" + CNAME + e);
}
}
private static void readPropertyFiles() throws FileNotFoundException, IOException {
prop.load(new FileInputStream(location));
noOfThreads = Integer.parseInt(prop.getProperty("NUMBER_OF_THREADS").trim());
noOfTasks = Integer.parseInt(prop.getProperty("NUMBER_OF_TASKS").trim());
startRange = Integer.parseInt(prop.getProperty("ID_START_RANGE").trim());
tableNames = Arrays.asList(prop.getProperty("TABLES").trim().split(","));
for (String arg : tableNames) {
//Other Code
}
}
問題陳述: -
現在我試圖做的,如果我傳遞等是 - 從命令提示符假設參數如NUMBER_OF_THREADS, NUMBER_OF_TASKS, ID_START_RANGE, TABLES
以及config.properties file
,那麼它應該覆蓋config.properties file
的值。所以,如果我跑我的程序像這個 -
java -jar Test.jar "C:\\test\\config.properties" t:10 n:100 i:2 TABLES:TABLE1 TABLES:TABLE2 TABLES:TABLE3
然後在我的程序 -
noOfThreads should be 10 instead of 100
noOfTasks should be 100 instead of 10000
startRange should be 2 instead of 1
tableNames should have three table TABLE1, TABLE2, TABLE3 instead of TABLE1 and TABLE2.
以上格式,如果我需要覆蓋config.property文件我將隨之而來。
但如果我正在像這個 -
java -jar Test.jar "C:\\test\\config.properties"
那麼它應該讀一切從config.properties file
。
如果我在命令行中傳遞參數以及config.property
文件位置,通常我想覆蓋config.properties file
。
任何人都可以提供一個例子(乾淨的方式)做這種情況?
謝謝克里斯的建議。我在問題中提到的格式是唯一的格式,如果我需要覆蓋這些值,我將使用它。你能爲我提供一個我如何實現這個目標的例子嗎?謝謝您的幫助。 – AKIWEB 2013-03-18 02:03:39