Java 从JFileChooser获取file.properties并在ResourceBundle上使用它
目前,我正在构建一个Java桌面应用程序,用户可以通过JFileChooser加载file.properties来设置语言。但是,它引发了一个异常:找不到基本名称language.properties、locale pt_BR的捆绑包 我的文件名是language.properties,所以我不知道出了什么问题。我想加载默认的language.properties文件,而不是language_en.properties文件。这是我的密码:Java 从JFileChooser获取file.properties并在ResourceBundle上使用它,java,swing,file,resourcebundle,Java,Swing,File,Resourcebundle,目前,我正在构建一个Java桌面应用程序,用户可以通过JFileChooser加载file.properties来设置语言。但是,它引发了一个异常:找不到基本名称language.properties、locale pt_BR的捆绑包 我的文件名是language.properties,所以我不知道出了什么问题。我想加载默认的language.properties文件,而不是language_en.properties文件。这是我的密码: JFileChooser fileChooser = n
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);
}
任何帮助都将不胜感激。我更正了这个问题。我正在设置URL数组的绝对路径,但我应该只设置路径 以下是正确的代码:
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);
}
我将进一步改进。谢谢。我更正了这个问题。我正在设置URL数组的绝对路径,但我应该只设置路径 以下是正确的代码:
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);
}
我将进一步改进。谢谢。如果它特别指出“找不到基本名称语言.properties的捆绑包”,那么您在这里发布的代码片段是不正确的:您正在调用
getBundle(“language.properties”)
,您应该在这里调用getBundle(“language”)
,与snippet中的代码片段相同。另外,尚不清楚您的选择器是否需要选择文件或文件夹。如果所选文件未命名为“language.properties”,该怎么办?我已经更新了我的问题。我明白了File.getName()
将不会返回有效的捆绑包名称,正如我已经说过的:它将返回language.properties
。感谢您的输入。我现在在代码上修正了它,并再次更新了问题。它获取不带扩展名的文件名。但仍然是相同的错误。如果它特别指出“找不到基本名称语言.properties的bundle”,那么您在这里发布的代码片段是不正确的:您正在调用getBundle(“language.properties”)
,您应该在其中调用getBundle(“language”)
,与snippet中的代码片段相同。另外,尚不清楚您的选择器是否需要选择文件或文件夹。如果所选文件未命名为“language.properties”,该怎么办?我已经更新了我的问题。我明白了File.getName()
将不会返回有效的捆绑包名称,正如我已经说过的:它将返回language.properties
。感谢您的输入。我现在在代码上修正了它,并再次更新了问题。它获取不带扩展名的文件名。但还是有同样的错误。