Advertisement
Guest User

Untitled

a guest
Jun 29th, 2020
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. private static void copy(InputStream source, File dest) {
  2. try {
  3. OutputStream out = new FileOutputStream(dest);
  4. byte[] b = new byte[1024];
  5. int len;
  6.  
  7. while((len = source.read(b)) > 0) out.write(b, 0, len);
  8.  
  9. out.close();
  10. source.close();
  11. }
  12. catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. }
  16.  
  17. /**
  18. *
  19. * @param source file to copy from
  20. * @param dest file to override
  21. */
  22. public static void copy(File source, File dest) {
  23. try
  24. {
  25. InputStream in = new FileInputStream(source);
  26. copy(in, dest);
  27. }
  28. catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32.  
  33. /**
  34. *
  35. * @param source folder to copy from
  36. * @param dest folder to override
  37. */
  38. public static void copyFolder(File source, File dest) {
  39. if (!source.isDirectory()) return;
  40.  
  41. dest.mkdirs();
  42.  
  43. File[] files = source.listFiles();
  44. if (files == null || files.length == 0) return;
  45.  
  46. for (File file : files)
  47. {
  48. if (file.isDirectory()) {
  49. copyFolder(file, new File(dest + File.separator + file.getName()));
  50. }
  51. else {
  52. copy(file, new File(dest, file.getName()));
  53. }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement