【问题标题】:save file from filechooser to my courent directory of project将文件从文件选择器保存到我的项目目录
【发布时间】:2013-08-20 15:25:52
【问题描述】:

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

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

}

【问题讨论】:

  • 也许 JNDI 可以帮助查找项目文件夹
  • 我读了一点,谢谢,我想加载或保存,...,以及任何帮助我使用该文件的方式(选择的文件)

标签: java user-interface filechooser


【解决方案1】:

你的意思是?

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

【讨论】:

  • 感谢回复 :) 我使用此代码,但 eclip 给我错误“java.nio.Files”无法解析为类型。上述代码的第 2 行出现另一个错误。“java .nio.file.StandardCopyOption.COPY_ATTRIBUTES.nio 无法解析或不是字段”和语法错误:(
  • 好的,我已经修正了错别字。从初始输入中删除了一个逗号和一个单词。
  • 非常感谢,但不起作用:(,我的工作是真的吗?我复制了这段代码并在 File file=fc.getSelectedFile(); code in public void actionPerformed(ActionEvent e)功能,是真的吗?
猜你喜欢
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
相关资源
最近更新 更多