1

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

但我找不到任何东西可以帮助我将该文件复制或加载到我的项目并用于我的 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);
    }

}
4

1 回答 1

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.getName() 替换为您要命名的文件。

于 2013-08-20T16:39:56.960 回答