Reputation: 511
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
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