Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将文件从filechooser保存到项目的my courent目录_Java_User Interface_Filechooser - Fatal编程技术网

Java 将文件从filechooser保存到项目的my courent目录

Java 将文件从filechooser保存到项目的my courent目录,java,user-interface,filechooser,Java,User Interface,Filechooser,我想将选择filechooser的文件复制到我的项目目录 将此代码用于我的文件选择器 但我找不到任何东西来帮助我复制或加载该文件到我的项目中并用于我的ui,我在做什么 这是我的代码: @SuppressWarnings("serial") public class FileChooser extends JPanel implements ActionListener { static private final String newline = "\n"; JButton op

我想将选择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);
    }

}
你是说

/**
* 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。

也许JNDI可以帮助查找project folderi我读了一些关于它的内容,谢谢,我想加载或保存,…,以及帮助我使用所选文件的任何方法谢谢:我使用此代码,但是eclipe给我一个错误java.nio.Files不能作为一个类型来解析。另外一个错误是上面代码的2个结尾行。java.nio.file.StandardCopyOption.COPY\u ATTRIBUTES.nio不能解析或者不是一个字段和语法错误:好的,我已经修复了错误。从初始输入中删除了一个逗号和一个单词。非常感谢,但不是工作:,我的工作是真的吗?我复制此代码,并在文件File=fc.getSelectedFile之后通过它;代码在public void actionPerformedActionEvent e函数中,是否为真?