Guest User

Untitled

a guest
Mar 24th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class FileManager {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. String from = "E:\\JAVA\\Luxoft Traning\\JVA-008\\Path2";
  8. String to = "E:\\JAVA\\Luxoft Traning\\JVA-008\\Path3";
  9.  
  10. try {
  11. copy(from, to);
  12. } catch (IOException | NullPointerException e) {
  13. e.printStackTrace();
  14. }
  15. }
  16.  
  17. public static void copy(String from, String to) throws IOException, NullPointerException {
  18.  
  19. File source = new File(from);
  20. File destination = new File(to);
  21.  
  22. InputStream inputStream;
  23. OutputStream outputStream;
  24.  
  25. if (source.isFile()) {
  26. inputStream = new FileInputStream(source);
  27. outputStream = new FileOutputStream(destination);
  28.  
  29. byte[] buffer = new byte[1024];
  30. int bufferSize;
  31. while ((bufferSize = inputStream.read(buffer)) > 0) {
  32. outputStream.write(buffer, 0, bufferSize);
  33. }
  34. inputStream.close();
  35. outputStream.close();
  36.  
  37. } else {
  38.  
  39. if (!destination.exists()) {
  40. destination.mkdir();
  41. }
  42. String files[] = source.list();
  43.  
  44. for (String file : files) {
  45. File sourceFile = new File(source, file);
  46. File destinationFile = new File(destination, file);
  47. copy(sourceFile.getCanonicalPath(), destinationFile.getCanonicalPath());
  48. }
  49. }
  50. }
  51. }
Add Comment
Please, Sign In to add comment