繁体   English   中英

如何使用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的绝对路径,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM