Class FilePathParser

java.lang.Object
eu.svjatoslav.commons.file.FilePathParser

public class FilePathParser extends Object
Utility class for parsing file paths and extracting file metadata.

This class provides static methods for extracting file extensions, file names without extensions, and formatting file sizes for display. All methods are simple utilities that operate on file names or paths.

Key features:

  • Extract file extensions (lowercase normalized)
  • Get file names without extensions
  • Format file sizes with appropriate binary units (b, KiB, MiB, GiB, TiB, PiB)

Example usage:


 // Get file extension
 String ext = FilePathParser.getFileExtension("document.PDF");
 // Result: "pdf" (lowercase)

 // Get file name without extension
 String name = FilePathParser.getFileNameWithoutExtension(new File("image.jpg"));
 // Result: "image"

 // Format file size
 String size = FilePathParser.getFileSizeDescription(15728640);
 // Result: "15 MiB"
 

Note on extensions: For files with multiple dots, only the last dot-separated segment is considered the extension. For example, "archive.tar.gz" has extension "gz", not "tar.gz".

  • Method Details

    • getFileExtension

      public static String getFileExtension(File file)
      Returns the file extension from a File object.

      The extension is extracted from the file name (not the full path) and returned in lowercase. Files without an extension return an empty string.

      Example results:

      • "document.TXT" → "txt"
      • "image.JPG" → "jpg"
      • "archive.tar.gz" → "gz"
      • "README" → "" (empty string)
      Parameters:
      file - the file to extract the extension from. Must not be null.
      Returns:
      the file extension in lowercase, or an empty string if the file has no extension (no dot in the name, or dot at the start).
    • getFileExtension

      public static String getFileExtension(String fullFileName)
      Returns the file extension from a file name string.

      The extension is extracted from the last dot-separated segment and returned in lowercase. File names without an extension return an empty string.

      Example results:

      • "document.TXT" → "txt"
      • "archive.tar.gz" → "gz"
      • ".gitignore" → "gitignore"
      • "noextension" → ""
      • "file." → ""
      Parameters:
      fullFileName - the file name (not full path) to parse. Must not be null.
      Returns:
      the file extension in lowercase, or an empty string if the file has no extension.
    • getFileNameWithoutExtension

      public static String getFileNameWithoutExtension(File file)
      Returns the file name without its extension from a File object.

      The base name is everything before the last dot. If the file has no extension, the entire file name is returned.

      Example results:

      • "document.txt" → "document"
      • "archive.tar.gz" → "archive.tar"
      • "README" → "README"
      • "file." → "file"
      Parameters:
      file - the file to extract the base name from. Must not be null.
      Returns:
      the file name without the extension. Returns the complete file name if there is no extension.
    • getFileNameWithoutExtension

      public static String getFileNameWithoutExtension(String fullFileName)
      Returns the file name without its extension from a file name string.

      The base name is everything before the last dot. If the file has no extension, the entire file name is returned.

      Example results:

      • "document.txt" → "document"
      • "archive.tar.gz" → "archive.tar"
      • ".gitignore" → ""
      • "noextension" → "noextension"
      Parameters:
      fullFileName - the file name (not full path) to parse. Must not be null.
      Returns:
      the file name without the extension. Returns the complete file name if there is no extension.
    • getFileSizeDescription

      public static String getFileSizeDescription(long fileSize)
      Returns a human-readable description of a file size using binary units.

      The size is formatted with appropriate binary units (IEC standard). Larger units are used only when the size is at least 5 units of that size:

      Size unit thresholds
      UnitThreshold
      bytes (b)< 5 KiB (5120 bytes)
      kibibytes (KiB)≥ 5 KiB, < 5 MiB
      mebibytes (MiB)≥ 5 MiB, < 5 GiB
      gibibytes (GiB)≥ 5 GiB, < 5 TiB
      tebibytes (TiB)≥ 5 TiB, < 5 PiB
      pebibytes (PiB)≥ 5 PiB

      Example results:

      • 500 bytes → "500 b"
      • 1024 bytes → "1024 b"
      • 5120 bytes (5 KiB) → "5 KiB"
      • 1048576 bytes (1 MiB) → "1024 KiB"
      • 5242880 bytes (5 MiB) → "5120 KiB"
      • 5242881 bytes → "5 MiB"
      • 1073741824 bytes (1 GiB) → "1024 MiB"

      Note: This method uses binary units (KiB = 1024 bytes, MiB = 1024 KiB, etc.) per the IEC standard, not SI units (KB = 1000 bytes).

      Parameters:
      fileSize - the file size in bytes. Negative values will produce unexpected results.
      Returns:
      a human-readable size description with binary units.