David
David

Reputation: 511

How do you get the absolute paths for multiple file selections using jfilechooser from an array of file names in java

below is the source code for attaching multiple files.

public void doAttachFile(){
 try {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setMultiSelectionEnabled(true);
        int selection = fileChooser.showOpenDialog(null);
        if(selection == JFileChooser.APPROVE_OPTION){// if open button is clicked
            File [] selectedFile = fileChooser.getSelectedFiles();
        }
}catch(Exception e){
     JOptionPane.showMessageDialog(this,"Error attaching files\n"+e.toString,"Error",JOptionPane.ERROR_MESSAGE);
}

}

How do you get the absolute paths for the selected files from the array?

Upvotes: 1

Views: 785

Answers (1)

Vasu
Vasu

Reputation: 22452

You can iterate through each File object and get the absolute path of the file as shown below:

File [] selectedFile = fileChooser.getSelectedFiles();
for(File file : selectedFile) {
    String absolutePath = file.getAbsolutePath(); //gives the absolute path
    System.out.println(absolutePath);
}

Upvotes: 2

Related Questions