[英]getting file.properties from JFileChooser and using it on ResourceBundle
Currently I am building a Java Desktop Application where the user can load a file.properties through JFileChooser to set the language. 目前我正在构建一个Java桌面应用程序,用户可以通过JFileChooser加载file.properties来设置语言。 However, it throws me an exception: Can't find bundle for base name language.properties, locale pt_BR. 但是,它抛出了一个异常:找不到基本名称language.properties,locale pt_BR的包。
My file name is language.properties so I don't know what is wrong. 我的文件名是language.properties,所以我不知道出了什么问题。 I want to load the default language.properties file and not language_en.properties for example. 我想加载默认的language.properties文件而不是language_en.properties。 Here is my code: 这是我的代码:
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
URL[] urls = null;
try {
urls= new URL[]
{
selectedFile.toURI().toURL()
};
} catch (MalformedURLException e){// TODO Auto-generated catch block
e.printStackTrace();
}
String fileName = selectedFile.getName();
int pos = fileName.lastIndexOf(".");
if (pos > 0) {
fileName = fileName.substring(0, pos);
}
ClassLoader loader = new URLClassLoader(urls);
ResourceBundle bundle = ResourceBundle.getBundle(fileName,Locale.getDefault(),loader);
}
Any help would be greatly appreciated. 任何帮助将不胜感激。
I corrected the issue. 我纠正了这个问题。 I was setting the absolute path to the urls array but I should have set only the path. 我设置了urls数组的绝对路径,但我应该只设置路径。
Here is the right code: 这是正确的代码:
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
int pos2 = selectedFile.getAbsolutePath().lastIndexOf(selectedFile.getName());
String path = null;
path = selectedFile.getAbsolutePath().replace(selectedFile.getName(), "");
File file = new File(path);
URL[] urls = null;
try {
urls=new URL[]{
file.toURI().toURL()
};}
catch (MalformedURLException e){// TODO Auto-generated catch block
e.printStackTrace();
}
String fileName = selectedFile.getName();
int pos = fileName.lastIndexOf(".");
if(pos > 0){
fileName = fileName.substring(0,pos);
}
ClassLoader loader = new URLClassLoader(urls);
bundle = ResourceBundle.getBundle(fileName, Locale.getDefault(), loader);
}
I will make further improvements. 我会进一步改进。 Thank you. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.