2013-04-01 2 views
0

어딘가에 텍스트 파일을 만드는 간단한 자바 프로그램을 만들고 싶습니다. 사용자가 폴더를 선택할 수있게하고 싶습니다. 하지만 jar 파일로 포장하고 다른 사람들에게 보냅니다. 내 친구가 그것을 실행할 때마다 폴더를 묻습니다. 항아리 파일에 한 번만 묻고 마지막으로 설정하도록 할 수있는 방법이 궁금합니다. 내 된 .java 파일 내부 , 나는 당신이 어딘가에 사용자 설정을 저장해야사용자 기본 설정

public void setDefaultFolder() { 
    //initially path is empty string 
    if(fc.getPath().equals("")){ 
     String a = JOptionPane.showInputDialog("Please select a default folder." + 
       " Make sure it exists and it's not changeable once set"); 
     if(a != null) { 
     String b = a.replace("\\","\\\\"); 
     // set String path = something users enter 
     // fc is a helper object. I have all the read/write file or other methods 
     // there. I want to let the jar file read the path only for the first time 
     // and set the path final, later on, when it detects path is not empty 
     // string, it stops asking users to type in path. 
     fc.setPath(b); 
     } 
     else { 
      JOptionPane.showMessageDialog(null, "Folder not set"); 
     } 
    } 
} 
+0

구성 파일에 저장하십시오. 구성 파일이 있으면 경로를 읽으십시오. 그렇지 않다면 요청하여 저장하십시오. – m0skit0

+0

if 문을 비우는 방법을 추가하는 방법, JOpitionPane.showInputDialog를 실행하도록하고 싶습니다. 비어 있지 않으면 입력 대화 상자를 실행하지 않습니다. –

답변

2

같은 것을 가지고있다. 당신이 패키지를 java.util.prefs의 사용 예를 들어 자신의 answer to this question 피터 Knego에 의해 명시된 바와 같이이 작업을 수행하는 방법에는 여러 가지가,있다 :

// Retrieve the user preference node for the package com.mycompany 
Preferences prefs = Preferences.userNodeForPackage(com.mycompany.MyClass.class); 

// Preference key name 
final String PREF_NAME = "name_of_preference"; 

// Set the value of the preference 
String newValue = "a string"; 
prefs.put(PREF_NAME, newValue); 

// Get the value of the preference; 
// default value is returned if the preference does not exist 
String defaultValue = "default string"; 
String propertyValue = prefs.get(PREF_NAME, defaultValue); // "a string" 
+0

새 수업을 만들어야합니까? –

+0

아니요,이 부분을 확인해보세요. http://alvinalexander.com/blog/post/java/simple-java-preferences-api-example –

+0

if 문을 빈 채로 추가하는 방법, JOpitionPane.showInputDialog를 실행하도록하고 싶습니다. 비어 있지 않으면 입력 대화 상자를 실행하지 않습니다. –

0

당신이 속성 폴더 위치를 저장하는 파일을 사용 할 수 있습니다. jar가로드 될 때마다이 등록 정보를로드합니다.

+0

당신이 체크 아웃 할 것을 제안합니다 ... [props useage] (http://docs.oracle.com/javase/tutorial/essential/environment/properties.html) 및 [props 예제] (http://www.mkyong.com/java/java-properties-file-examples /) –