2009年3月12日 星期四

Java - choose file

JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".xls") //限定顯示.xls為結尾的檔案
f.isDirectory();
}
public String getDescription() {
return "Excel files";
}
});

int r = chooser.showOpenDialog(new JFrame());
if (r == JFileChooser.APPROVE_OPTION) {
name = chooser.getSelectedFile().getPath();// .getName(); //getpath回傳檔案路徑加上檔案名稱,getname只回傳檔案名稱
// System.out.println(name);
}

沒有留言: