选择文件与获取选中文件绝对路径

来源:互联网 发布:jdbc连接数据库 编辑:程序博客网 时间:2024/06/05 12:06
public static void main(String[] args) {JFrame frame = new JFrame();JButton button = new JButton("上传");frame.add(button);frame.setVisible(true);button.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {// 按钮点击事件JFileChooser chooser = new JFileChooser(); // 设置选择器chooser.setMultiSelectionEnabled(false); // 设为单选int returnVal = chooser.showOpenDialog(button); // 是否打开文件选择框System.out.println("returnVal=" + returnVal);if (returnVal == JFileChooser.APPROVE_OPTION) { // 如果符合文件类型String filepath = chooser.getSelectedFile().getAbsolutePath(); // 获取绝对路径System.out.println(filepath);System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); // 输出相对路径}}});}