jfilechooser - set directory to a path in a file

fermk090 picture fermk090 · Apr 19, 2011 · Viewed 48.7k times · Source

I am trying to set the directory path in JFilechooser through something like this(using commons-io ) :

String fileContents = IOUtils.toString(new FileInputStream("path.txt"));
File theDirectory = new File(fileContents);

filechooser = new JFileChooser();
fileChooser.setCurrentDirectory(theDirectory);
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

I'm using getCanonicalPath() to get the path and write in the file path.txt

path = file.getCanonicalPath();

I don't intend to put all my code here,but I'm sure that the program writes and reads the path in path.txt. I don't get any error,but everytime I run the program it always open JFilechooser in my documents folder.What i am doing wrong?

Answer

Heisenbug picture Heisenbug · Apr 19, 2011

Try to pass the current directory directly in the constructor:

filechooser = new JFileChooser(theDirectory);