简体   繁体   English

使用Swing JFileChooser复制文件

[英]Copying file with swing JFileChooser

Hello i am trying to make a simple application which will copy some file to some location. 您好,我正在尝试制作一个简单的应用程序,它将一些文件复制到某个位置。

I made two JFileChoosers first one which choose file which we want to copy and the second one which choose destination where we want to copy file. 我做了两个JFileChoosers,第一个选择要复制的文件,第二个选择要复制文件的目的地。 Now i dont know how to selected file to the selected location. 现在我不知道如何选择文件到所选位置。 Here is the code so far. 这是到目前为止的代码。

public class MainFrame extends JFrame
{
private JButton btnSelectFile = new JButton("Select File"); // Kada se pritisne selectFile dugme, otvori se fileChooser.
private JLabel lbSelectFile = new JLabel("waiting"); // Ispisuje putanju do fajla koji zelimo da kopiramo.
private JButton btnWhereToCopy = new JButton("Where to copy"); // Kada pritisnemo dugme izadje file chooser da izaberemo gde zelimo da kopiramo file
private JLabel lbWhereToCopy = new JLabel("waiting"); // Stoji putanja gde zelimo da kopiramo file
private JButton btnCopyFiles = new JButton("Copy Files"); // Dugme kada se pritisne kopira fajlove
private JLabel lbIsCopied = new JLabel("waiting"); // Ako se fajl uspesno kopirao ispise se na ovoj labeli, a ako nije ispise se error.

private JPanel panel = new JPanel(); // Drzacemo pointer koji fajl zelimo da otvorimo.

private File selectedFile = null;
private File whereToCopy = null;

private ActionListener selectFile = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0)
    {
        JFileChooser fc = new JFileChooser(); // Otvorimo file chooser.

        // showOpenDialog vraca int. Moguce su opcije, CANCEL,APPROVE i ERROR. Mi moramo da proverimo koji se nama option desio od ova 3
        int returnValue = fc.showOpenDialog(null); 

        if(returnValue == JFileChooser.APPROVE_OPTION) // Proverimo da li imamo approve, da se file mogao lepo otvoriti, ako jeste radimo sa njim sta zelimo.
        {
            selectedFile = fc.getSelectedFile(); // Stavimo pointer na fajl koji smo izabrali
            lbSelectFile.setText(selectedFile.getAbsolutePath()); // Ispisemo u labelu putanju fajla, cisto da znate sta ste izabrali.
        }
    }
};

private ActionListener selectPath = new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e)
    {
        JFileChooser fc = new JFileChooser(); // Otvaramo file chooser da izaberemo mesto gde zelimo da kopiramo file
        fc.setCurrentDirectory(new java.io.File("."));
        fc.setDialogTitle("Where to copy file");
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fc.setAcceptAllFileFilterUsed(false);


        if(fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
        {
            whereToCopy = fc.getSelectedFile();
            lbWhereToCopy.setText(whereToCopy.getAbsolutePath());
        }
        else
        {
            System.out.println("No selection");
        }
    }
};

private ActionListener copyFile = new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(selectedFile == null || whereToCopy == null)
        {
            lbIsCopied.setText("You didnt select file path");
            return;
        }

        // HOW TO COPY THIS FILES NOW
};

public MainFrame()
{
    super();
    setSize(new Dimension(500, 400));
    setTitle("Copy Applicaton");

    // Centrira aplikaciju na centar ekrana
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    add(getPanel());
    pack();
}

private JPanel getPanel()
{
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // Y_AXIS, stavlja elemente po Y osi.
    panel.setPreferredSize(new Dimension(500, 400));


    btnSelectFile.addActionListener(selectFile); // Obradjuje dogadjaj dugmeta.
    btnWhereToCopy.addActionListener(selectPath); // Obradjuje dogadjaj dugmeta
    btnCopyFiles.addActionListener(copyFile); // Obradjuje dogadjaje

    // Dodajemo elemente na panel, bice nam poredjane u zavisnosti koji smo layout koristili, nas slusaj ja BoxLayout
    panel.add(btnSelectFile);
    panel.add(lbSelectFile);
    panel.add(btnWhereToCopy);
    panel.add(lbWhereToCopy);
    panel.add(btnCopyFiles);
    panel.add(lbIsCopied);
    return panel;
}

} }

I have three ActionListeners, one which gets file from JFileChooser and the second one which gets where we want to copy file. 我有三个ActionListener,一个是从JFileChooser获取文件的,另一个是我们要复制文件的位置。 I dont know how to make third ActionListener which will copy file. 我不知道如何使第三个ActionListener将复制文件。 Any help ? 有什么帮助吗?

This is what i did so far but i got error. 这是我到目前为止所做的,但是我遇到了错误。

        try {
            Files.copy(selectedFile.toPath(), whereToCopy.toPath(), StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            lbIsCopied.setText("Error");
            e1.printStackTrace();
            return;
        }

        lbIsCopied.setText("Did it");
    }

I always get error. 我总是会出错。 AccessDeniedException error. AccessDeniedException错误。

Try the renameTo() method 试试renameTo()方法

try{
    File orginalFile = new File("path original file");
    File newFilePath = new File("path to new directory");

    if (originalFile.renameTo(newFilePath + File.Seperator + originalFile.getName())){
        System.out.println(true);
    }else {
        System.out.println(false);
    }
} catch (Exception e){
    E.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM