是附加多个文件的源代码。
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);
}
}
如何从阵列中获取所选文件的绝对路径?
答案 0 :(得分:2)
您可以遍历每个File
对象并获取文件的绝对路径,如下所示:
File [] selectedFile = fileChooser.getSelectedFiles();
for(File file : selectedFile) {
String absolutePath = file.getAbsolutePath(); //gives the absolute path
System.out.println(absolutePath);
}