Search code examples
javastringswingfilenamesjfilechooser

JFileChooser getCurrentDirectory() to String


I have this code:

JFileChooser openFolder = new JFileChooser();
openFolder.setCurrentDirectory(new java.io.File("."));
openFolder.setDialogTitle("Select target directory");
openFolder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
openFolder.setAcceptAllFileFilterUsed(false);
if (openFolder.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{      
    File newLoc = openFolder.getCurrentDirectory();   
}

How can I make it so that It converts this:

    File newLoc = openFolder.getCurrentDirectory();

To a String if its possible?

For example using the FileChooser I chose the folder: C:\Music

I tried using:

String locToString = FileUtils.readFileToString(newLoc);

but it doesn't work.

I want to convert it to string so I can make it appear on a JTextField using:

jTextField.setText(locToString);

Solution

  • newLoc.getAbsolutePath() will give you a String from a File, per the javadoc.