package learntocode.javaapi.io_and_nio;

import java.io.File;
import java.io.IOException;

public class FilesCreator {

    private static String dirIOFiles = "/tmp/test_files_io";
    private static String dirNIOFiles = "/tmp/test_files_nio";

    private static String fileNameTemplate = "file_";

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



    public static void createFilesIO () {
        createFiles(dirIOFiles);
    }

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

    private static void createFiles(String dir) {
        int numOfFiles = 10;

        File directoryFile = new File(dirIOFiles);
        directoryFile.mkdirs();

        for (int i =0; i < numOfFiles; i++) {
            try {
                File f = new File(dirIOFiles, fileNameTemplate + i);
                if (f.exists()) {
                    f.delete();
                }
                boolean fileCreated = f.createNewFile();
                if (!fileCreated) {
                    System.out.println("Could not create file " + fileNameTemplate + i + " in directory " + dirIOFiles);
                    break;
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

@@ -84,16 +84,214 @@
 ption;%0A%0A
+/**%0A * Provide methods to create directories with files on you machine.%0A * Is used in every class io and nio packages examples%0A * %3Cbr%3E%0A * Change directories in this file to ones you like better%0A */%0A
 public c
@@ -351,31 +351,8 @@
 iles
- = %22/tmp/test_files_io%22
 ;%0A  
@@ -390,32 +390,8 @@
 iles
- = %22/tmp/test_files_nio%22
 ;%0A%0A 
@@ -651,18 +651,16 @@
 ate; %7D%0A%0A
-%0A%0A
     publ
@@ -689,24 +689,47 @@
 ilesIO () %7B%0A
+        setDirsByOs();%0A
         crea
@@ -1212,32 +1212,116 @@
               %7D%0A
+                System.out.print(%22Creating file %22 + f.getAbsolutePath() + %22 ... %22);%0A
                 
@@ -1437,16 +1437,24 @@
 rintln(%22
+ ERROR! 
 Could no
@@ -1553,16 +1553,93 @@
  break;%0A
+                %7D else %7B%0A                    System.out.println(%22Success!%22);%0A
         
@@ -1644,25 +1644,24 @@
           %7D%0A
-%0A
             
@@ -1742,21 +1742,750 @@
  %7D%0A        %7D
+%0A%0A        System.out.println(%22Initialization finished. Your output: %5Cn%22);%0A    %7D%0A%0A%0A    private static void setDirsByOs() %7B%0A        String os = System.getProperty(%22os.name%22);%0A        if (os.startsWith(%22Windows%22)) %7B%0A            System.out.println(%22Current OS is Windows%22);%0A            String homeDrive = System.getProperty(%22user.home%22);%0A            dirIOFiles = homeDrive + %22%5C%5Ctest_files_io%22;%0A            dirNIOFiles = homeDrive + %22%5C%5Ctest_files_nio%22;%0A        %7D else %7B%0A            dirIOFiles = %22/tmp/test_files_io%22;%0A            dirNIOFiles = %22/tmp/test_files_nio%22;%0A        %7D%0A%0A        System.out.println(%22Setting test files directories to: %5Cn%22 +%0A                %22  IO: %22 + dirIOFiles + %22%5Cn%22 +%0A                %22  NIO: %22 + dirNIOFiles);
 %0A    %7D%0A%7D%0A

Create file examples should now work both for linux and windows