Class DirectoryCreator

java.lang.Object
hlcd.operations.DirectoryCreator

public class DirectoryCreator extends Object
The creation of directories and files is taken care of here. All created folder and files will contain a timestamp at the end. There will not be any deletion or overriding of data. In case the main path is empty, then the directory where the project is being executed will be used as the default location. Also, empty folder names or file names will be assigned a random hexadecimal string of eight characters. Using the following:
 DirectoryCreator dc = new DirectoryCreator("path...", "test");
 dc.createDirectory();//required!
to create a directory. The convention is to create a new folder as soon as an instance is created.
 //minimal example to execute this class:
 public static void main(String[] args) {
     System.out.println("Running DirectoryCreator...");
     String path = "";//Paths.DEFAULT_PATH;
     String directoryName = "";
     boolean addTimestamp = false;
     boolean override = true;
     boolean appendTimestamp = true;
     boolean printToConsole = true;
     DirectoryCreator dc = new DirectoryCreator(
         path, directoryName, printToConsole
     );
     dc.createDirectory();
     BufferedWriter f1 = dc.getNewCreatedFile(
         "", ".txt", appendTimestamp, printToConsole
     );
     BufferedWriter f2 = dc.getNewCreatedFile(
         "", ".txt", appendTimestamp, printToConsole
     );
     try {
         //First file
         f1.write("Some text in the first file...");
         f1.newLine();
         f1.flush();
         f1.close();
         //Second file
         f2.write("Some text in the second file...");
         f2.newLine();
         f2.write("More text :)!");
         f2.flush();
         f2.close();
     } catch (IOException e) {
         e.printStackTrace();
     }
     System.out.println("DirectoryCreator completed.");
 }
Since:
February 24th, 2022
Version:
1.0
Author:
Maysara Al Jumaily
  • Constructor Details

    • DirectoryCreator

      public DirectoryCreator(String path, String directoryName)
      Initializes the object based on the path specified with the passed directory name. It will by default print the directory created on the console.
      Parameters:
      path - the path to create the new folder in
      directoryName - the name of the new folder to create
    • DirectoryCreator

      public DirectoryCreator(String path, String directoryName, boolean printToConsole)
      Initializes the object based on the path specified with the passed directory name.
      Parameters:
      path - the path to create the new folder in
      directoryName - the name of the new folder to create
      printToConsole - should the path of the new folder to create be displayed on the console
  • Method Details

    • createDirectory

      public void createDirectory()
      Creates a new folder.
    • getNewCreatedFile

      public BufferedWriter getNewCreatedFile(String path, String filename, String extension, boolean appendTimestamp, boolean printToConsole)
      Creates a new file in the path specified based on the other data passed.
      Parameters:
      path - the path to create the file in
      filename - the filename of the new file to be created
      extension - the extension of the new file to be created (for example .txt)
      appendTimestamp - should a timestamp be added to the name
      printToConsole - should the path of the new file to be created be displayed on the console
      Returns:
      the newly created file object
    • getNewCreatedFile

      public BufferedWriter getNewCreatedFile(String filename, String extension, boolean appendTimestamp, boolean printToConsole)
      Creates a new file in the path specified based on the other data passed.
      Parameters:
      filename - the filename of the new file to be created
      extension - the extension of the new file to be created (for example .txt)
      appendTimestamp - should a timestamp be added to the name
      printToConsole - should the path of the new file to be created be displayed on the console
      Returns:
      the newly created file object
    • getNewCreatedFile

      public BufferedWriter getNewCreatedFile(String filename, String extension, boolean appendTimestamp)
      Creates a new file in the path specified based on the other data passed. It will display the file path to console by default.
      Parameters:
      filename - the filename of the new file to be created
      extension - the extension of the new file to be created (for example .txt)
      appendTimestamp - should a timestamp be added to the name
      Returns:
      the newly created file object
    • getCompletePath

      public String getCompletePath()
      Returns the complete path of the directory created.
      Returns:
      the complete path of the directory created
      See Also:
      getDirectoryName()
    • getDirectoryName

      public String getDirectoryName()
      Returns the current directory name that was created.
      Returns:
      the current directory name that was created
      See Also:
      getCompletePath()