package learntocode.javaapi.io_and_nio.nio;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Provide methods to create directories with files on you machine.
 * Is used in every class io and nio packages examples
 * <br>
 * Change directories in this file to ones you like better
 */ /*
public class FilesCreatorNIO {

    private static String dirIOFiles;
    private static String dirNIOFiles;

    private static String fileNameTemplate = "file_";

    public static String getDirNIOFiles() { return dirNIOFiles; }
    public static String getFileNameTemplate() { return fileNameTemplate; }

    public static void createFilesNIO() {
        createFiles(dirNIOFiles);
    }

    private static File[] createFiles(String dir) {
        int numOfFiles = 10;
        Path[] files = new Path[numOfFiles];
        Path directoryFile = Paths.get(dirIOFiles);
        //directoryFile.

        for (int i =0; i < numOfFiles; i++) {
            try {
                File f = new File(dirIOFiles, fileNameTemplate + i);
                if (f.exists()) {
                    f.delete();
                }
                System.out.print("Creating file " + f.getAbsolutePath() + " ... ");
                boolean fileCreated = f.createNewFile();
                if (!fileCreated) {
                    System.out.println(" ERROR! Could not create file " + fileNameTemplate + i + " in directory " + dirIOFiles);
                    break;
                } else {
                    files[i] = f;
                    System.out.println("Success!");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Initialization finished. Your output: \n");
        return files;
    }


    private static void setDirsByOs() {
        String os = System.getProperty("os.name");
        if (os.startsWith("Windows")) {
            System.out.println("Current OS is Windows");
            String homeDrive = System.getProperty("user.home");
            dirNIOFiles = homeDrive + "\\test_files_nio";
        } else {
            dirNIOFiles = "/tmp/test_files_nio";
        }

        System.out.println("Setting test files directories to: \n" +
                "  NIO: " + dirNIOFiles);
    }
}
*/
@@ -53,20 +53,27 @@
 java.io.
-File
+IOException
 ;%0Aimport
@@ -74,38 +74,38 @@
 import java.
+n
 io.
-IOException
+file.Files
 ;%0Aimport jav
@@ -351,11 +351,8 @@
 %0A */
- /*
 %0Apub
@@ -384,46 +384,8 @@
  %7B%0A%0A
-    private static String dirIOFiles;%0A
     
@@ -636,20 +636,22 @@
  static 
-void
+Path%5B%5D
  createF
@@ -654,19 +654,16 @@
 ateFiles
-NIO
 () %7B%0A   
@@ -671,92 +671,22 @@
     
-createFiles(dirNIOFiles);%0A    %7D%0A%0A    private static File%5B%5D createFiles(String dir) %7B
+setDirsByOs();
 %0A   
@@ -794,24 +794,25 @@
 aths.get(dir
+N
 IOFiles);%0A  
@@ -821,242 +821,99 @@
     
-//directoryFile.%0A%0A        for (int i =0; i %3C numOfFiles; i++) %7B%0A            try %7B%0A                File f = new File(dirIOFiles, fileNameTemplate + i);%0A                if (f.exists()) %7B%0A                    f.delete();%0A                %7D
+try %7B%0A            Files.createDirectories(directoryFile);%0A        %7D catch (IOException e) %7B
 %0A   
@@ -913,36 +913,32 @@
 ) %7B%0A            
-    
 System.out.print
@@ -941,112 +941,102 @@
 rint
+ln
 (%22C
-reating file %22 + f.getAbsolutePath() + %22 ... %22);%0A                boolean fileCreated = f.createNewFil
+ould not create directories to%22 + directoryFile.toString());%0A            e.printStackTrac
 e();
@@ -1044,24 +1044,27 @@
         
+%7D%0A%0A
         
 if (!fil
@@ -1059,24 +1059,42 @@
     
-if (!fileCreated
+for (int i =0; i %3C numOfFiles; i++
 ) %7B%0A
@@ -1109,280 +1109,313 @@
     
- 
+try %7B%0A
        
-System.out.println(%22 ERROR! Could not create file %22 + fileNameTemplate + i + %22 in directory %22 + dirIOFiles);%0A                    break;%0A                %7D else %7B%0A                    files%5Bi%5D = f;%0A                    System.out.println(%22Success!%22);%0A                %7D
+         Path f = Paths.get(dirNIOFiles, fileNameTemplate + i);%0A                if (Files.exists(f)) %7B%0A                    Files.delete(f);%0A                %7D%0A                System.out.println(%22Creating file %22 + f.toString() + %22 ... Success!%22);%0A                files%5Bi%5D = Files.createFile(f);
 %0A   
@@ -2118,10 +2118,8 @@
     %7D%0A%7D%0A
-*/

Add FileCreatorNIO which uses Paths/Files nio classes to create directories