java中的属性文件

时间:2016-08-23 10:31:02

标签: java

我正在尝试在java中运行以下示例代码。

import java.util.Locale;  
import java.util.ResourceBundle;

public class InternationalizationDemo {  

  public static void main(String[] args) {        
    ResourceBundle bundle = ResourceBundle.getBundle("MessageBundle", Locale.CANADA_FRENCH);  
    System.out.println("Message in "+Locale.CANADA_FRENCH +":"+bundle.getString("greeting"));
  }
}

1.当MessageBundle.properties放在类路径中时,上面的代码可以正常执行。 但是我想通过从类路径中删除MessageBundle.properties并将其放在其他位置来成功执行上述代码。 我怎样才能做到这一点? 在此先感谢。

2 个答案:

答案 0 :(得分:2)

您可以使用PropertyResourceBundle并从import threading import requests import json import random exitFlag = 0 class myThread (threading.Thread): def __init__(self, threadID, name, key): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.key = key def run(self): print "Starting " + self.name do_bet(self.name, self.key) print "Exiting " + self.name def do_bet(threadName, key): while True: #do post on api def change_seed(): payload = {'seed': random.randrange(100, 1000, 2)} requests.post('https://url.com/api?api_key=' + str(key), data=payload) def high(amount): payload = {'amount': amount, 'target': '90.00', 'condition': '<'} bet = requests.post('https://api.url.com/api?api_key=' + str(key), data=payload) win_bet = bet.json()['bet']['win'] roll_bet = bet.json()['bet']['roll'] return win_bet, roll_bet def low(amount): payload = {'amount': amount, 'target': '9.99', 'condition': '>'} bet = requests.post('https://api.url.com/api?api_key=' + str(key), data=payload) win_bet = bet.json()['bet']['win'] roll_bet = bet.json()['bet']['roll'] return win_bet, roll_bet def onloss(horl): if horl == 0: win, roll = low(100) if win == False: change_seed() win, roll = high(1000) if win == False: low(0) elif horl == 1: win, roll = high(100) if win == False: change_seed() win, roll = low(1000) if win == False: high(0) win, roll = high(10) if win == False: onloss(0) win, roll = low(10) if win == False: onloss(1) # Create new threads thread1 = myThread(1, "First", "8asd8a-hd7837hda-837g78-a37rjf3") thread2 = myThread(2, "Two", "f56fah-sddddjf74f-kkkdj-fdbf446") # Start new Threads thread1.start() thread2.start() 属性获取属性文件的路径,例如:

System

然后在启动命令中,您需要添加String configPath = System.getProperty("config.path"); ResourceBundle bundle = new PropertyResourceBundle(new FileReader(configPath));

答案 1 :(得分:1)

您可以通过以下方式在外部加载属性文件:

// Path to your file, if you have it on local, use something like C:\\MyFolder or \home\usr\etc
File file = new File("YOUR_PATH");
URL[] url = {file.toURI().toURL()};
ResourceBundle rb = ResourceBundle.getBundle("MessageBundle", Locale.CANADA_FRENCH, new URLClassLoader(url));

示例来自此处:https://coderanch.com/t/432762/java/java/absolute-path-bundle-file

您可以使用保存在本地的远程属性文件或文件。