Skip to content

System Utilities Documentation

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

Introduction

Retrieving system-related properties and file paths is essential for managing applications effectively. Whether you need to access the user's home directory, temporary files, or system properties, the file-worker library provides robust tools for working with system-level information.

Imagine building an application that dynamically configures itself based on the user's environment. This library makes accessing such system details seamless.

Features Overview

  • Retrieve user-specific directories such as home, temp, and downloads.
  • Fetch system properties like OS name, Java version, and file separators.
  • Work with platform-independent system paths.

Getting Started

To include this library in your Java project, add the following dependency:

Gradle Dependency

Replace the variable ${latestVersion} with the current latest version:
Maven Central

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")

Using the libs.versions.toml File

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

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>

Retrieving System File Paths with SystemFileExtensions

The SystemFileExtensions class provides methods for retrieving system-related file paths.

Example: Getting the User's Home Directory

File homeDir = SystemFileExtensions.getUserHomeDir();
System.out.println("User Home Directory: " + homeDir.getAbsolutePath());

Example: Getting the System Temp Directory

File tempDir = SystemFileExtensions.getTempDir();
System.out.println("Temporary Directory: " + tempDir.getAbsolutePath());

Unit Test Coverage

The SystemFileExtensionsTest class ensures correctness:

@Test
public void testGetUserHomeDir() {
    File homeDir = SystemFileExtensions.getUserHomeDir();
    assertNotNull(homeDir);
}

Retrieving System Properties with SystemPropertiesExtensions

The SystemPropertiesExtensions class provides methods for retrieving system properties.

Example: Getting OS and Java Information

String osName = SystemPropertiesExtensions.getOsName();
String javaVersion = SystemPropertiesExtensions.getJavaVersion();

System.out.println("Operating System: " + osName);
System.out.println("Java Version: " + javaVersion);

Example: Fetching the System File Separator

String separator = SystemPropertiesExtensions.getFileSeparator();
System.out.println("File Separator: " + separator);

Unit Test Coverage

The SystemPropertiesExtensionsTest class ensures property retrieval works:

@Test
public void testGetJavaVersion() {
    String javaVersion = SystemPropertiesExtensions.getJavaVersion();
    assertNotNull(javaVersion);
}

Working with System Paths

The library allows retrieving and working with platform-independent system paths.

Example: Getting the User Downloads Directory

File downloadsDir = SystemFileExtensions.getUserDownloadsDir();
System.out.println("Downloads Directory: " + downloadsDir.getAbsolutePath());

Example: Getting the Root Directory

File rootDir = SystemFileExtensions.getRootDir();
System.out.println("Root Directory: " + rootDir.getAbsolutePath());

Unit Test Coverage

@Test
public void testGetRootDir() {
    File rootDir = SystemFileExtensions.getRootDir();
    assertNotNull(rootDir);
}

Setting System Properties

The library allows modifying system properties dynamically.

Example: Setting a Custom System Property

Properties properties = new Properties();
properties.setProperty("app.mode", "debug");

SystemPropertiesExtensions.setSystemProperties(properties);

System.out.println("App Mode: " + System.getProperty("app.mode"));

Unit Test Coverage

@Test
public void testSetSystemProperties() {
    Properties props = new Properties();
    props.setProperty("test.prop", "value");

    SystemPropertiesExtensions.setSystemProperties(props);
    assertEquals("value", System.getProperty("test.prop"));
}

Conclusion

The file-worker library provides efficient utilities for accessing system-related directories and properties, making it a valuable tool for platform-independent development.

Start using it today to enhance your application's system interactions!

Clone this wiki locally