复制文件时如何使用ProgressMonitorInputStream?

时间:2012-04-01 13:20:39

标签: java swing file-io copy progress-bar

我正在尝试将文件从一个地方复制到另一个地方。我正在使用FileInputStreamFileOutputStream要复制。这是我的代码:

private JPanel contentPane;
private JButton btnCopy;
private JFileChooser SelectFile;
private JFileChooser ChooseDestination;
private File Selectedfile;
private File chosenDestination;


    protected void Copy() {
        // TODO Auto-generated method stub
        int answer = SelectFile.showOpenDialog(this);
        if(answer==0){
            this.Selectedfile = SelectFile.getSelectedFile();
            int answer2 = ChooseDestination.showSaveDialog(this);
            if(answer2==0){
                this.chosenDestination = ChooseDestination.getSelectedFile();
                try {
                    FileInputStream Read = new FileInputStream(Selectedfile);
                    FileOutputStream write = new FileOutputStream(chosenDestination + Selectedfile.getName());
                    InputStream in = new BufferedInputStream(new ProgressMonitorInputStream(main.this,"Copying", new FileInputStream(Selectedfile)));
                    int Data;
                    byte[] buffer = new byte[1024];
                    while((Data= in.read(buffer))>0){
                        write.write(buffer);  
                    }//Here the progressmMonitorInputStream window opens but there is nothing in the window.
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

直到文件副本完成为止。但文件正确复制。我只想在单独窗口中显示进度条。请帮我。谢谢。

2 个答案:

答案 0 :(得分:4)

以下代码可以提供帮助:

import java.awt.BorderLayout;

public class Main extends JFrame implements PropertyChangeListener {

    private JPanel contentPane;
    private JButton btnCopy;
    private JFileChooser selectFile;
    private JFileChooser chooseDestination;
    private File selectedfile;
    private File chosenDestination;
    private JProgressBar progressBar;
    private Copy copy;

    class Copy extends SwingWorker<Void, Void> {
        @Override
        protected Void doInBackground() throws Exception {
            int answer = selectFile.showOpenDialog(Main.this);
            if (answer == 0) {
                Main.this.selectedfile = selectFile.getSelectedFile();
                int answer2 = chooseDestination.showSaveDialog(Main.this);
                if (answer2 == 0) {
                    Main.this.chosenDestination = chooseDestination
                            .getSelectedFile();
                    try {
                        FileInputStream fileInputStream = new FileInputStream(
                                selectedfile);
                        BufferedInputStream bufferedInputStream = new BufferedInputStream(
                                fileInputStream);
                        ProgressMonitorInputStream progressMonitorInputStream = new ProgressMonitorInputStream(
                                Main.this, "Copying", bufferedInputStream);
                        File outputFile = new File(chosenDestination
                                + selectedfile.getName());
                        FileOutputStream fileOutputStream = new FileOutputStream(
                                outputFile);
                        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                                fileOutputStream);
                        int data;
                        byte[] buffer = new byte[1024];
                        while ((data = progressMonitorInputStream.read(buffer)) > 0) {
                            bufferedOutputStream.write(buffer);                         }
                        bufferedOutputStream.close();
                        progressMonitorInputStream.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return null;
        }

        /*
         * Executed in event dispatching thread
         */
        @Override
        public void done() {
            Toolkit.getDefaultToolkit().beep();
            btnCopy.setEnabled(true);
            setCursor(null); // turn off the wait cursor
        }
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * 
     * @throws UnsupportedLookAndFeelException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws ClassNotFoundException
     */
    public Main() throws ClassNotFoundException, InstantiationException,
            IllegalAccessException, UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 130, 135);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        progressBar = new JProgressBar(0, 100);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);
        panel.setLayout(new MigLayout("", "[89px][][][][][][][]", "[23px][][]"));

        btnCopy = new JButton("Copy");
        btnCopy.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                copy = new Copy();
                copy.addPropertyChangeListener(Main.this);
                copy.execute();
            }
        });
        panel.add(btnCopy, "cell 1 1,grow");
        selectFile = new JFileChooser();
        chooseDestination = new JFileChooser();
        chooseDestination.setFileSelectionMode(2);
        pack();
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress" == evt.getPropertyName()) {
            int progress = (Integer) evt.getNewValue();
            progressBar.setValue(progress);
        }
    }
}

答案 1 :(得分:3)