簡體   English   中英

使用Swing JFileChooser復制文件

[英]Copying file with swing JFileChooser

您好,我正在嘗試制作一個簡單的應用程序,它將一些文件復制到某個位置。

我做了兩個JFileChoosers,第一個選擇要復制的文件,第二個選擇要復制文件的目的地。 現在我不知道如何選擇文件到所選位置。 這是到目前為止的代碼。

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;
}

}

我有三個ActionListener,一個是從JFileChooser獲取文件的,另一個是我們要復制文件的位置。 我不知道如何使第三個ActionListener將復制文件。 有什么幫助嗎?

這是我到目前為止所做的,但是我遇到了錯誤。

        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");
    }

我總是會出錯。 AccessDeniedException錯誤。

試試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