Skip to content

Create New Files And Directories

Asterios Raptis edited this page Feb 10, 2025 · 4 revisions

Introduction

Managing files and directories efficiently is crucial in many applications. Whether you're working with configuration files, managing structured data, or automating file processing tasks, the file-worker library provides a robust set of utilities to simplify these operations.

Imagine you're building a document management system that needs to create, manipulate, and store files dynamically. This library will help you handle these tasks efficiently with minimal boilerplate code.

Features Overview

The file-worker library offers:

  • Simple methods to create files and directories.
  • Utility classes for handling file contents and metadata.
  • Support for structured directory management.

Getting Started

For installation and dependencies, refer to the Getting Started guide.


Now, let’s explore how to use this library in real-world scenarios.


Creating Files and Directories with Createable

The Createable interface provides a straightforward way to create files and directories.

Example: Creating a Single File

Createable createable = new Createable() {};
File file = new File("example.txt");
createable.newFile(file);

System.out.println("File created successfully!");

Example: Creating Multiple Directories

List<File> directories = List.of(new File("logs"), new File("data"), new File("backup"));
createable.newDirectories(directories);

System.out.println("Directories created successfully!");

Managing File Metadata with FileContentInfo

The FileContentInfo class stores file metadata such as checksum and content.

Example: Retrieving File Metadata

File file = new File("example.txt");
FileContentInfo fileInfo = FileContentInfo.toFileContentInfo(file);

System.out.println("Checksum: " + fileInfo.getChecksum());

Unit Test Coverage

The FileContentInfoTest class verifies the correctness of this functionality:

@Test
public void testToFileContentInfo() throws IOException {
    File file = new File("test.txt");
    FileContentInfo fileInfo = FileContentInfo.toFileContentInfo(file);
    
    assertNotNull(fileInfo.getChecksum());
}

Enum: FileCreationState

The FileCreationState enum represents the outcome of file creation operations.

  • ALREADY_EXISTS - The file or directory already exists.
  • CREATED - Successfully created.
  • FAILED - Creation failed.
  • PENDING - The operation is pending.

Creating Directory Structures with DirectoryFactory

The DirectoryFactory class provides an efficient way to manage directories.

Example: Creating a Temporary Directory

File tempDir = DirectoryFactory.newTempDirectory("tempDir");
System.out.println("Temporary directory created: " + tempDir.getAbsolutePath());

Example: Creating Nested Directories

File nestedDir = new File("data/logs/errors");
DirectoryFactory.newDirectories(nestedDir.toPath());

System.out.println("Nested directories created successfully!");

Unit Test Coverage

The DirectoryFactoryTest class includes comprehensive tests:

@Test
public void testNewDirectoryWithParentFileAndDirectoryName() throws IOException {
    File parent = new File("parentDir");
    File dir = DirectoryFactory.newDirectory(parent, "subDir");

    assertTrue(dir.exists());
    assertTrue(dir.isDirectory());
}

Managing Directory Structures with DirectoryStructureFactory

The DirectoryStructureFactory class helps in managing structured directories.

Example: Creating a Directory Structure

File rootDir = new File("project");
List<String> subDirs = List.of("src", "bin", "lib");

DirectoryStructureFactory.newDirectoryStructure(rootDir, subDirs);

System.out.println("Project directory structure created!");

Unit Test Coverage

The DirectoryStructureFactoryTest class ensures correct behavior:

@Test
public void testNewDirectoryStructure() throws IOException {
    File root = new File("app");
    List<String> paths = List.of("config", "logs", "data");

    Collection<File> createdDirs = DirectoryStructureFactory.newDirectoryStructure(root, paths);
    assertEquals(3, createdDirs.size());
}

Extracting File Information with DirectoryStructureExtensions

The DirectoryStructureExtensions class helps retrieve file metadata from directory structures.

Example: Retrieving File Information

File dir = new File("data");
List<FileContentInfo> files = DirectoryStructureExtensions.getFileContentInfos(dir);

System.out.println("Found " + files.size() + " files in directory.");

Unit Test Coverage

The DirectoryStructureExtensionsTest verifies this functionality:

@Test
public void testGetFileContentInfos() throws IOException {
    File directory = new File("sampleDir");
    List<FileContentInfo> fileInfos = DirectoryStructureExtensions.getFileContentInfos(directory);

    assertNotNull(fileInfos);
}

Creating Files with FileFactory

The FileFactory class provides an easy way to create files with various configurations.

Example: Creating a File with a Parent Directory

File parent = new File("config");
File file = FileFactory.newFile(parent, "settings.json");

System.out.println("Config file created successfully!");

Unit Test Coverage

The FileFactoryTest ensures correct behavior:

@Test
public void testNewFile() throws IOException {
    File file = FileFactory.newFile("test.txt");
    assertTrue(file.exists());
}

Conclusion

The file-worker library provides a powerful set of utilities for managing files and directories efficiently. Whether you're developing a file-based application, processing logs, or working with structured data, this library simplifies file handling with minimal effort.

Start using it today to streamline your file management tasks!

Clone this wiki locally