package io.github.xxyy.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

/**
 * Class that provides some static utilities to work
 * with files.
 * 
 * @author <a href="http://xxyy.github.io/">xxyy</a>
 */
public abstract class FileHelper
{
    /**
     * Copies a file or directory {@code src} (and all its contents, if any)
     * to a new destination {@code dest}.
     * @throws IOException If something went wrong while copying
     */
    public static void copyFolder(File src, File dest) throws IOException{
        if(src.isDirectory())
        {
            if(!dest.exists())
            {
               assert dest.mkdirs();
            }
     
            String files[] = src.list();
     
            for (String file : files) 
            {
               File srcFile = new File(src, file);
               File destFile = new File(dest, file);
               FileHelper.copyFolder(srcFile,destFile);
            }
     
        }
        else
        {
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest); 
            byte[] buf = new byte[1024];
 
            int length;
            while ((length = in.read(buf)) > 0)
            {
               out.write(buf, 0, length);
            }
 
            in.close();
            out.close();
        }
    }
    
    /**
     * Deletes a file or directory and all its contents.
     * @param pathToDir a {@link Path} pointing to the directory to be deleted.
     * @throws java.io.IOException If a file could not be deleted.
     */
    public static void deleteAll(Path pathToDir) throws IOException{
        Files.walkFileTree(pathToDir, new SimpleFileVisitor<Path>() {
            
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                if (exc == null) {
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                }
                throw exc;
            }
       
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }
       
        });
    }
}

@@ -49,162 +49,9 @@
 .io.
-File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream
+*
 ;%0Aim
@@ -308,17 +308,16 @@
 iles.%0A *
- 
 %0A * @aut
@@ -398,17 +398,17 @@
 leHelper
-%0A
+ 
 %7B%0A    /*
@@ -528,16 +528,23 @@
  dest%7D.%0A
+     *%0A
      * @
@@ -673,32 +673,33 @@
 rows IOException
+ 
 %7B%0A        if(src
@@ -694,16 +694,17 @@
       if
+ 
 (src.isD
@@ -714,24 +714,16 @@
 ctory())
-%0A       
  %7B%0A     
@@ -731,16 +731,17 @@
       if
+ 
 (!dest.e
@@ -748,31 +748,20 @@
 xists())
-%0A           
  %7B%0A
+ 
         
@@ -795,37 +795,32 @@
 ;%0A            %7D%0A
-     
 %0A            Str
@@ -845,21 +845,16 @@
 list();%0A
-     
 %0A       
@@ -884,31 +884,19 @@
  files) 
+%7B
 %0A 
-           %7B%0A
         
@@ -930,32 +930,33 @@
 ile(src, file);%0A
+ 
                F
@@ -984,32 +984,33 @@
 le(dest, file);%0A
+ 
                F
@@ -1038,16 +1038,17 @@
 srcFile,
+ 
 destFile
@@ -1068,36 +1068,104 @@
   %7D%0A
+%0A
      
-%0A        %7D%0A        else
+   %7D else %7B%0A            if (!dest.exists()) %7B%0A                assert dest.createNewFile();
 %0A   
@@ -1161,33 +1161,38 @@
 File();%0A        
-%7B
+    %7D%0A
 %0A            Inp
@@ -1188,16 +1188,21 @@
         
+try (
 InputStr
@@ -1236,16 +1236,21 @@
 m(src);%0A
+     
         
@@ -1302,10 +1302,12 @@
 est)
-; 
+) %7B%0A
 %0A   
@@ -1315,16 +1315,20 @@
         
+    
 byte%5B%5D b
@@ -1348,17 +1348,16 @@
 %5B1024%5D;%0A
- 
 %0A       
@@ -1361,16 +1361,20 @@
         
+    
 int leng
@@ -1389,16 +1389,20 @@
         
+ 
+   
 while ((
@@ -1428,16 +1428,18 @@
 f)) %3E 0)
+ %7B
 %0A       
@@ -1443,14 +1443,8 @@
     
-    %7B%0A
     
@@ -1447,27 +1447,24 @@
             
-   
 out.write(bu
@@ -1494,12 +1494,19 @@
     
-%7D%0A %0A
+    %7D%0A%0A    
     
@@ -1533,24 +1533,28 @@
             
+    
 out.close();
@@ -1546,32 +1546,46 @@
    out.close();%0A
+            %7D%0A
         %7D%0A    %7D%0A
@@ -1584,20 +1584,16 @@
 %7D%0A    %7D%0A
-    
 %0A    /**
@@ -1650,16 +1650,23 @@
 ntents.%0A
+     *%0A
      * @
@@ -1879,16 +1879,17 @@
 xception
+ 
 %7B%0A      
@@ -1952,28 +1952,16 @@
 th%3E() %7B%0A
-            
 %0A       
@@ -2255,39 +2255,32 @@
 ;%0A            %7D%0A
-       
 %0A            @Ov
@@ -2489,23 +2489,16 @@
       %7D%0A
-       
 %0A       

Create files if they don't exist while copying in FileHelper