Package hlcd.operations
Class DirectoryCreator
java.lang.Object
hlcd.operations.DirectoryCreator
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 Summary
ConstructorsConstructorDescriptionDirectoryCreator(String path, String directoryName)Initializes the object based on the path specified with the passed directory name.DirectoryCreator(String path, String directoryName, boolean printToConsole)Initializes the object based on the path specified with the passed directory name. -
Method Summary
Modifier and TypeMethodDescriptionvoidCreates a new folder.Returns the complete path of the directory created.Returns the current directory name that was created.getNewCreatedFile(String filename, String extension, boolean appendTimestamp)Creates a new file in the path specified based on the other data passed.getNewCreatedFile(String filename, String extension, boolean appendTimestamp, boolean printToConsole)Creates a new file in the path specified based on the other data passed.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.
-
Constructor Details
-
DirectoryCreator
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 indirectoryName- the name of the new folder to create
-
DirectoryCreator
Initializes the object based on the path specified with the passed directory name.- Parameters:
path- the path to create the new folder indirectoryName- the name of the new folder to createprintToConsole- 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 infilename- the filename of the new file to be createdextension- the extension of the new file to be created (for example.txt)appendTimestamp- should a timestamp be added to the nameprintToConsole- 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 createdextension- the extension of the new file to be created (for example.txt)appendTimestamp- should a timestamp be added to the nameprintToConsole- 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 createdextension- 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
Returns the complete path of the directory created.- Returns:
- the complete path of the directory created
- See Also:
getDirectoryName()
-
getDirectoryName
Returns the current directory name that was created.- Returns:
- the current directory name that was created
- See Also:
getCompletePath()
-