-
-
Notifications
You must be signed in to change notification settings - Fork 1
Create New Files And Directories
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.
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.
For installation and dependencies, refer to the Getting Started guide.
Now, let’s explore how to use this library in real-world scenarios.
The Createable interface provides a straightforward way to create files and directories.
Createable createable = new Createable() {};
File file = new File("example.txt");
createable.newFile(file);
System.out.println("File created successfully!");List<File> directories = List.of(new File("logs"), new File("data"), new File("backup"));
createable.newDirectories(directories);
System.out.println("Directories created successfully!");The FileContentInfo class stores file metadata such as checksum and content.
File file = new File("example.txt");
FileContentInfo fileInfo = FileContentInfo.toFileContentInfo(file);
System.out.println("Checksum: " + fileInfo.getChecksum());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());
}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.
The DirectoryFactory class provides an efficient way to manage directories.
File tempDir = DirectoryFactory.newTempDirectory("tempDir");
System.out.println("Temporary directory created: " + tempDir.getAbsolutePath());File nestedDir = new File("data/logs/errors");
DirectoryFactory.newDirectories(nestedDir.toPath());
System.out.println("Nested directories created successfully!");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());
}The DirectoryStructureFactory class helps in managing structured directories.
File rootDir = new File("project");
List<String> subDirs = List.of("src", "bin", "lib");
DirectoryStructureFactory.newDirectoryStructure(rootDir, subDirs);
System.out.println("Project directory structure created!");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());
}The DirectoryStructureExtensions class helps retrieve file metadata from directory structures.
File dir = new File("data");
List<FileContentInfo> files = DirectoryStructureExtensions.getFileContentInfos(dir);
System.out.println("Found " + files.size() + " files in directory.");The DirectoryStructureExtensionsTest verifies this functionality:
@Test
public void testGetFileContentInfos() throws IOException {
File directory = new File("sampleDir");
List<FileContentInfo> fileInfos = DirectoryStructureExtensions.getFileContentInfos(directory);
assertNotNull(fileInfos);
}The FileFactory class provides an easy way to create files with various configurations.
File parent = new File("config");
File file = FileFactory.newFile(parent, "settings.json");
System.out.println("Config file created successfully!");The FileFactoryTest ensures correct behavior:
@Test
public void testNewFile() throws IOException {
File file = FileFactory.newFile("test.txt");
assertTrue(file.exists());
}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!
- Create New Files And Directories
- Delete Files Or Directories
- File Comparison Documentation
- File Modification And Merging
- File Reading Documentation
- File Renaming Documentation
- File Search Documentation
- File Sorting Documentation
- File Writing Documentation
- Java File Copy Utilities
- System Utilities Documentation