将文件从jfilechooser保存到我当前的项目目录

时间:2013-08-20 15:25:52

标签: java user-interface filechooser

我想将选择了filechooser的文件复制到我的项目目录中 将此代码用于我的文件选择器,

但我找不到任何可以帮助我复制或加载该文件到我的项目并使用我的ui的东西,我在做什么?

我的代码:

@SuppressWarnings("serial")
public class FileChooser extends JPanel implements ActionListener {
    static private final String newline = "\n";
    JButton openButton, saveButton;
    JTextArea log;
    JFileChooser fc;

    public FileChooser() {
        super(new BorderLayout());
        log = new JTextArea(5,20);
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(false);
        JScrollPane logScrollPane = new JScrollPane(log);

        fc = new JFileChooser();

        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        openButton = new JButton("Open a File...",
                createImageIcon("images/Open16.gif"));
        openButton.addActionListener(this);

        saveButton = new JButton("Save a File...",
                createImageIcon("images/Save16.gif"));

        saveButton.addActionListener(this);
        JPanel buttonPanel = new JPanel(); //use FlowLayout
        buttonPanel.add(openButton);
        openButton.setBackground(new java.awt.Color(255,255,255));
        buttonPanel.add(saveButton);
        saveButton.setBackground(new java.awt.Color(255,255,255));
        add(buttonPanel, BorderLayout.PAGE_START);
        buttonPanel.setBackground(new java.awt.Color(255,255,255));
        add(logScrollPane, BorderLayout.CENTER);
    }
    private File file;

    public void actionPerformed(ActionEvent e) {
        boolean isOpen=false;
        //Handle open button action.
        if (e.getSource() == openButton) {
            int returnVal = fc.showOpenDialog(FileChooser.this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = fc.getSelectedFile();
                //This is where a real application would open the file.
                log.append("Opening: " + file.getName() + "." + newline);
                isOpen=true;
            } else {
                log.append("Open command cancelled by user." + newline);
            }
            log.setCaretPosition(log.getDocument().getLength());

            //Handle save button action.
        } else if (e.getSource() == saveButton && isOpen) {
            System.out.println(file.getAbsolutePath());
            File file=fc.getSelectedFile();
            new File(file.getName(),"./images/" );
            System.out.println("--"+file.getName());

            log.append("Saving: " + file.getName() + "." + newline);
            log.setCaretPosition(log.getDocument().getLength());
        }
    }
    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = FileChooser.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                UIManager.put("swing.boldMetal", Boolean.FALSE); 
                createAndShowGUI();
            }
        });
    }
    public static void createAndShowGUI() {
        JFrame frame = new JFrame("FileChooserDemo");
        frame.setDefaultCloseOperation(1);
        frame.add(new FileChooser());
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

}

1 个答案:

答案 0 :(得分:0)

你的意思是?

/**
* THE REST OF YOUR CODE
*/
java.nio.file.Files.copy( 
                   file.toPath(), 
                   new java.io.File(file.getName()).toPath(),
                   java.nio.file.StandardCopyOption.REPLACE_EXISTING,
                   java.nio.file.StandardCopyOption.COPY_ATTRIBUTES,
                   java.nio.file.LinkOption.NOFOLLOW_LINKS );

我以为你想确保File和以前一样。否则,将file.getName()替换为您要为文件命名的内容。