0

I want to make a simple java program that creates a text file at somewhere. I want to let users choose the folder. But when I wrap it in jar file and send to others. Every time my friend runs it, it asks for the folder. I wonder how I can let the jar file only asks once and set it as final. Inside my .java file, I have something like

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");
        }
    }
} 
Aubin
  • 14,617
  • 9
  • 61
  • 84
Fang Li
  • 59
  • 1
  • 1
  • 5
  • Save it in a configuration file. If the configuration file exists, read the path. If not, ask for it and save it. – m0skit0 Apr 01 '13 at 20:53
  • How to I add some if statement, if it is empty, the I want to let execute that JOpitionPane.showInputDialog. If it's not empty, then I am not executing the input dialog. – Fang Li Apr 02 '13 at 02:49

2 Answers2

2

You need to save the users configuration somewhere. There is many ways to do this, for example you can use java.util.prefs package, as stated by Peter Knego in his answer to this question:

// 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"
Community
  • 1
  • 1
0x6C38
  • 6,796
  • 4
  • 35
  • 47
  • Do I have to create a new class? – Fang Li Apr 01 '13 at 21:39
  • No, check this out, might help you out: http://alvinalexander.com/blog/post/java/simple-java-preferences-api-example – 0x6C38 Apr 01 '13 at 21:44
  • How to I add some if statement, if it is empty, the I want to let execute that JOpitionPane.showInputDialog. If it's not empty, then I am not executing the input dialog. – Fang Li Apr 02 '13 at 02:50
0

you might want to use a properties file to store the folder location. you would then load the property every time the jar loads

Andy B
  • 328
  • 3
  • 9
  • suggest you check out...[props useage](http://docs.oracle.com/javase/tutorial/essential/environment/properties.html) and [props example](http://www.mkyong.com/java/java-properties-file-examples/) – Andy B Apr 01 '13 at 20:55