簡體   English   中英

Java ResourceBundle

[英]Java ResourceBundle

我已經幾個月沒有使用Java編程了,但是我有一個小應用程序可以開發供自己使用。 我被困在ResourceBundle上! 我記得它非常易於使用,所以我想我的問題很容易解決,但是需要新的眼睛:)

Locale frLocale = new Locale("fr","CA");
Locale enLocale = new Locale("en","CA");

String dir = System.getProperty("user.dir");
System.out.println("current dir = " + dir); // current dir = D:\RP1.8.2

Path p = FileSystems.getDefault().getPath(dir,"test.properties");
System.out.println("Path : "+p.toAbsolutePath().toString()); // Path : D:\RP1.8.2\test.properties

boolean exists = Files.exists(p);
System.out.println("File found: "+exists); // File found: true

ResourceBundle bundle = ResourceBundle.getBundle(p.toAbsolutePath().toString());
/* Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name D:\RP1.8.2\test.properties, locale fr_CA */

String title = bundle.getString("main.title");
System.out.println("Titre = "+title);

所以我的屬性文件存在,但是ResourceBundle沒有找到它……很奇怪。 在用戶目錄中,我有3個文件:test.properties,test_fr.properties和test_fr_CA.properties

對我有什么線索嗎? 謝謝

getBundle的參數不是文件名,並且實際上一定不能是文件名。 ResourceBundle文件是應用程序資源,需要與編譯的類放在相同的位置。 (實際上,ResourceBundle可以是實際的Java類,而不是屬性文件。)例如:

build
└─classes
  └─com
    └─example
      └─myapp
        ├─MyWindow.class
        ├─MyDataImporter.class
        ├─test.properties
        ├─test_fr.properties
        └─test_fr_CA.properties

碼:

ResourceBundle bundle = ResourceBundle.getBundle("com.example.myapp.test");

如果它是一個Maven項目,那么我將右鍵單擊該項目,然后在“ M2工具”下選擇“生成工件(檢查更新)”,然后單擊“生成OSGI捆綁包”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM