// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.*;
import java.io.*;

import javax.swing.*;

public class CopyProgress extends JFrame {
   private JProgressBar pbar;
   public CopyProgress() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      getContentPane().setLayout(new BorderLayout());
      pbar = new JProgressBar();
      getContentPane().add(pbar, BorderLayout.CENTER);
      pack();
   }
   public void copy(String from, String to) {
      pbar.setMaximum((int)(new File(from)).length());
      pbar.setValue(0);
      pbar.setStringPainted(true);
      try {
         InputStream is = new FileInputStream(from);
         OutputStream os = new FileOutputStream(to);
         int total = 0;
         byte[] b = new byte[1000];
         int n;
         while((n = is.read(b)) >= 0) {
            os.write(b, 0, n);
            total += n;        
            pbar.setValue(total);
         }
         is.close();
         os.close();
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public static void main(String[] args) {
      CopyProgress cp = new CopyProgress();
      cp.show();
      cp.copy("C:\\z.1", "C:\\z.2");
   }
}
