Advertisement
Guest User

MyRun

a guest
Apr 4th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. package data;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9.  
  10. import javax.swing.JProgressBar;
  11.  
  12. public class MyRun implements Runnable {
  13.  
  14. private String source;
  15. private String destination;
  16. private JProgressBar progressBar;
  17.  
  18. public MyRun(String source, String destination, JProgressBar progressBar) {
  19. this.source = source;
  20. this.destination = destination;
  21. this.progressBar = progressBar;
  22.  
  23. }
  24.  
  25. @Override
  26. public void run() {
  27. {
  28. InputStream inStream = null;
  29. OutputStream outStream = null;
  30. try {
  31. File afile = new File(source);
  32. File bfile = new File(destination);
  33.  
  34. inStream = new FileInputStream(afile);
  35. outStream = new FileOutputStream(bfile);
  36. progressBar.setMinimum(0);
  37. progressBar.setMaximum(100);
  38. long length = afile.length();
  39. long counter = 0;
  40. int r = 0;
  41.  
  42. byte[] buffer = new byte[1024];
  43.  
  44. while ((r = inStream.read(buffer)) != -1) {
  45. counter += r;
  46. progressBar.setValue((int) (100 * counter / length));
  47. outStream.write(buffer, 0, r);
  48. }
  49.  
  50. inStream.close();
  51. outStream.close();
  52. System.out.println("Plik zostal skopiowany!");
  53.  
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement