如何使用 jfilechooser 从 java 中的文件名数组中获取多个文件选择的绝对路径

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

下面是附加多个文件的源代码。

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);
}

}

如何从数组中获取所选文件的绝对路径?

您可以遍历每个 File 对象并获取文件的绝对路径,如下所示:

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