Skip to content

io microsphere io FileUtils

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

FileUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.io | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/io/FileUtils.java

Overview

File Utility

Declaration

public abstract class FileUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

cleanDirectory

File dir = new File("/path/to/directory");
int deletedCount = cleanDirectory(dir);
System.out.println("Deleted " + deletedCount + " files/directories.");

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.io.FileUtils;

API Reference

Public Methods

Method Description
resolveRelativePath An empty immutable File array.
getFileExtension Gets the extension of a file name, if any.
deleteDirectory Deletes a directory and returns the number of deleted files and directories.
cleanDirectory Cleans a directory by deleting all files and sub-directories without deleting the directory itself.
forceDelete Deletes a file or directory and all its contents recursively.
forceDeleteOnExit Schedules a file or directory for deletion on JVM exit.
deleteDirectoryOnExit Schedules a directory for deletion on JVM exit, including all its contents.
listFiles Schedules all files and subdirectories within the given directory for deletion on JVM exit.
isSymlink Determines if the provided File is a symbolic link.
getCanonicalFile Returns the canonical form of the specified File.

Method Details

getFileExtension

public static String getFileExtension(String fileName)

Gets the extension of a file name, if any.

Example Usage

  • getFileExtension("file.txt") returns "txt"
  • getFileExtension("file.tar.gz") returns "gz"
  • getFileExtension(".hiddenfile") returns null (no extension)
  • getFileExtension("file") returns null (no extension)
  • getFileExtension("") returns null (blank string)
  • getFileExtension(null) returns null

deleteDirectory

public static int deleteDirectory(File directory)

Deletes a directory and returns the number of deleted files and directories.

If the directory does not exist, it is considered already deleted, and this method returns 0.

Example Usage

  • deleteDirectory(new File("/tmp/testDir")) deletes the directory and all its contents, returning the total count of deleted files and directories.
  • deleteDirectory(new File("/nonexistent/dir")) returns 0 since the directory does not exist.

cleanDirectory

public static int cleanDirectory(File directory)

Cleans a directory by deleting all files and sub-directories without deleting the directory itself.

This method recursively deletes all files and directories within the provided directory. If any file or sub-directory cannot be deleted, an IOException is thrown after attempting to delete as many as possible. 1

Example Usage

`File dir = new File("/path/to/directory");
int deletedCount = cleanDirectory(dir);
System.out.println("Deleted " + deletedCount + " files/directories.");
`

forceDelete

public static int forceDelete(File file)

Deletes a file or directory and all its contents recursively.

If the provided file is a directory, this method deletes all sub-directories and files, then deletes the directory itself. If it's a regular file, it deletes that single file.

Example Usage

  • forceDelete(new File("/tmp/file.txt")) deletes the file and returns 1
  • forceDelete(new File("/tmp/testDir")) deletes the directory and all its contents, returning the total count of deleted files and directories.
  • forceDelete(new File("/nonexistent/file")) throws a NoSuchFileException

forceDeleteOnExit

public static void forceDeleteOnExit(File file)

Schedules a file or directory for deletion on JVM exit.

If the provided file is a directory, this method schedules all sub-directories and files, then schedules the directory itself. If it's a regular file, it schedules that single file.

Example Usage

  • forceDeleteOnExit(new File("/tmp/file.txt")) schedules the file for deletion on exit.
  • forceDeleteOnExit(new File("/tmp/testDir")) schedules the directory and all its contents for deletion on exit.

deleteDirectoryOnExit

public static void deleteDirectoryOnExit(File directory)

Schedules a directory for deletion on JVM exit, including all its contents.

If the directory does not exist, this method does nothing. If it does exist, it schedules the directory for deletion and recursively schedules all files and subdirectories for deletion.

Example Usage

  • deleteDirectoryOnExit(new File("/tmp/testDir")) ensures that the directory and all its contents are deleted when the JVM exits.
  • deleteDirectoryOnExit(new File("/nonexistent/dir")) does nothing since the directory does not exist.

listFiles

public static File[] listFiles(File directory)

Schedules all files and subdirectories within the given directory for deletion on JVM exit.

This method does not delete the directory itself, only its contents. If the directory is a symbolic link, its contents will not be processed.

Example Usage

  • cleanDirectoryOnExit(new File("/tmp/testDir")) ensures that all contents of the directory are deleted when the JVM exits, but the directory itself remains.
  • cleanDirectoryOnExit(new File("/nonexistent/dir")) does nothing since the directory does not exist.

isSymlink

public static boolean isSymlink(File file)

Determines if the provided File is a symbolic link.

Example Usage

  • isSymlink(new File("/path/to/symlink")) returns true if it's a symbolic link.
  • isSymlink(new File("/path/to/regularfile")) returns false as it's not a symbolic link.
  • isSymlink(null) throws a NullPointerException.

getCanonicalFile

public static File getCanonicalFile(File file)

Returns the canonical form of the specified File.

This method wraps the call to File#getCanonicalFile() in a try-catch block to handle any checked exceptions via the io.microsphere.lang.function.ThrowableSupplier utility.

Example Usage

  • getCanonicalFile(new File("relative/path")) returns the canonical file object.
  • If the file does not exist or I/O error occurs, it will propagate as an unchecked exception.

See Also

  • FileUtils

This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally