-
-
Notifications
You must be signed in to change notification settings - Fork 1
File Reading Documentation
Reading files efficiently is crucial for many applications, whether it's loading configuration files, processing logs, or reading structured data. The file-worker library provides utilities for handling file content retrieval in a flexible and performance-optimized manner.
Imagine you need to load a configuration file, extract key values from a CSV, or read large text files line by line. This library enables these tasks with minimal code while maintaining efficiency.
- Read entire file content as a string or byte array.
- Retrieve specific lines or properties from a file.
- Count total lines in a file efficiently.
- Convert file content into structured objects.
To include this library in your Java project, add the following dependency:
Replace the variable ${latestVersion} with the current latest version:
Define version in the gradle.properties file:
fileWorkerVersion=${latestVersion}
or in the build.gradle ext area:
fileWorkerVersion = "${latestVersion}"
Then, add the dependency:
implementation("io.github.astrapi69:file-worker:$fileWorkerVersion")
For automatic version updates:
[versions]
file-worker-version=${latestVersion}
[libraries]
file-worker = { module = "io.github.astrapi69:file-worker", version.ref = "file-worker-version" }
Then, add:
implementation libs.file.worker
Maven dependency is now available on Sonatype. Check out the
Sonatype repository
for the latest snapshots and releases.
<properties>
...
<file-worker.version>${latestVersion}</file-worker.version>
...
</properties>
<dependencies>
...
<dependency>
<groupId>io.github.astrapi69</groupId>
<artifactId>file-worker</artifactId>
<version>${file-worker.version}</version>
</dependency>
...
</dependencies>The ReadFileExtensions class provides methods for extracting file content.
File file = new File("config.properties");
String content = ReadFileExtensions.fromFile(file);
System.out.println("File content: " + content);byte[] fileBytes = ReadFileExtensions.readFileToBytearray(file);
System.out.println("File size in bytes: " + fileBytes.length);String firstLine = ReadFileExtensions.readHeadLine("logfile.log");
System.out.println("First log entry: " + firstLine);The ReadFileExtensionsTest class verifies these functionalities:
@Test
public void testReadFromFile() throws IOException {
File file = new File("test.txt");
StoreFileExtensions.toFile(file, "Hello World");
String content = ReadFileExtensions.fromFile(file);
assertEquals("Hello World", content);
}For large files, counting lines efficiently is essential.
long lineCount = ReadFileExtensions.countAllLines(new File("bigdata.txt"));
System.out.println("Total lines: " + lineCount);@Test
public void testCountAllLines() throws IOException {
File file = new File("data.txt");
StoreFileExtensions.toFile(file, "Line 1
Line 2
Line 3");
long count = ReadFileExtensions.countAllLines(file);
assertEquals(3, count);
}The library allows reading properties from configuration files.
Properties properties = ReadFileExtensions.readPropertiesFromFile("config.properties");
String dbUrl = properties.getProperty("database.url");
System.out.println("Database URL: " + dbUrl);@Test
public void testReadPropertiesFromFile() throws IOException {
File propertiesFile = new File("config.properties");
Properties properties = new Properties();
properties.setProperty("app.name", "FileWorker");
StoreFileExtensions.toFile(propertiesFile, properties.toString());
Properties loadedProps = ReadFileExtensions.readPropertiesFromFile(propertiesFile.getAbsolutePath());
assertEquals("FileWorker", loadedProps.getProperty("app.name"));
}The library allows retrieving individual lines efficiently.
String thirdLine = ReadFileExtensions.readLine(new File("sample.txt"), 2);
System.out.println("Line 3: " + thirdLine);@Test
public void testReadLine() throws IOException {
File file = new File("test.txt");
StoreFileExtensions.toFile(file, "Line1
Line2
Line3");
String line = ReadFileExtensions.readLine(file, 1);
assertEquals("Line2", line);
}The file-worker library offers robust tools for reading and processing file content. Whether you're extracting configurations, reading large data files, or parsing properties, this library provides an efficient and reliable solution.
Start using it today to streamline your file handling operations!
- 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