2017-03-23 6 views
1

현재 사용자가 언어를 설정하기 위해 JFileChooser를 통해 file.properties를로드 할 수있는 Java Desktop Application을 구축 중입니다. 그러나 그것은 나를 예외로 던졌습니다 : 기본 이름 language.properties, locale pt_BR에 대한 번들을 찾을 수 없습니다.JFileChooser에서 file.properties 가져 오기 및 ResourceBundle에서 사용

내 파일 이름이 language.properties이므로 잘못된 것이 무엇인지 알지 못합니다. 예를 들어 language_en.properties가 아닌 기본 language.properties 파일을로드하려고합니다. 여기 내 코드는 다음과 같습니다.

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); 
} 

어떤 도움을 주시면 감사하겠습니다.

+0

"기본 이름 ** language.properties **에 대한 번들을 찾을 수 없습니다"라고 구체적으로 말하면 여기에 게시 한 코드 스 니펫이 올바르지 않습니다 :'getBundle ("language.properties")' 스니 j에서와 같이'getBundle ("language")'을 호출해야합니다. –

+0

또한 선택자가 파일이나 폴더를 선택해야하는지 여부가 불분명합니다. 그리고 선택된 파일의 이름이 "language.properties"가 아닌 경우 어떻게해야합니까? –

+0

내 질문을 업데이트했습니다. 알 겠어. –

답변

0

문제를 해결했습니다. 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); 
     } 

내가 더 개선 할 것입니다 :

여기에 올바른 코드입니다. 고맙습니다.

관련 문제