Advertisement
JackOUT

Untitled

Jan 23rd, 2022
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. package games.coob.skywars.model;
  2.  
  3. import java.io.*;
  4.  
  5. public final class FileUtil {
  6.     public static void copy(final File source, final File destination) throws IOException {
  7.         if (source.isDirectory()) {
  8.             if (!destination.exists()) {
  9.                 destination.mkdirs();
  10.             }
  11.  
  12.             final String[] files = source.list();
  13.             if (files == null)
  14.                 return;
  15.  
  16.             for (final String file : files) {
  17.                 final File newSource = new File(source, file);
  18.                 final File newDestination = new File(destination, file);
  19.                 copy(newSource, newDestination);
  20.             }
  21.         } else {
  22.             final InputStream inputStream = new FileInputStream(source);
  23.             final OutputStream outputStream = new FileOutputStream(destination);
  24.  
  25.             final byte[] buffer = new byte[1024];
  26.  
  27.             int length;
  28.  
  29.             while ((length = inputStream.read(buffer)) > 0) {
  30.                 outputStream.write(buffer, 0, length);
  31.             }
  32.  
  33.             inputStream.close();
  34.             outputStream.close();
  35.         }
  36.     }
  37.  
  38.     public static void delete(final File file) {
  39.         if (file.isDirectory()) {
  40.             final File[] files = file.listFiles();
  41.  
  42.             if (files == null)
  43.                 return;
  44.            
  45.             for (final File child : files) {
  46.                 delete(child);
  47.             }
  48.         }
  49.  
  50.         file.delete();
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement