Font size:      

VFSJFileChooser Tutorial


// create a file chooser
final VFSJFileChooser fileChooser = new VFSJFileChooser();

// configure the file dialog
fileChooser.setAccessory(new DefaultAccessoriesPanel(fileChooser));

// show the file dialog
RETURN_TYPE answer = fileChooser.showOpenDialog(null);

// check if a file was selected
if (answer == RETURN_TYPE.APPROVE)
     final FileObject aFileObject = fileChooser.getSelectedFile();

     // remove authentication credentials from the file path
     final String safeName = VFSUtils.getFriendlyName(aFileObject.toString());

     System.out.printf("%s %s", "You selected:", safeName);

Reading a file contents

The return type of the component is a FileObject. You can easily read the file contents by getting an inputstream from it.

// retrieve the selected file
FileObject aFileObject = fileChooser.getSelectedFile();

// retrieve an input stream and buffer it
InputStream is = VFSUtils.getInputStream(aFileObject);

Saving back the file

// retrieve the selected file
FileObject aFileObject = fileChooser.getSelectedFile();

// get an output stream and buffer it
OutputStream os = VFSUtils.getOutputStream(aFileObject);

Hiding the user credentials(remote connection)

By default the filename value contains the authentication credentials of the opened session. You can hide the password information in your application this way:

// get the file name without credentials
String safeName = VFSUtils.getFriendlyName(aFileObject.toString());