Class FileUtils

java.lang.Object
com.digitizer.core.FileUtils

public final class FileUtils extends Object
Utility class for file operations including filename sanitization and default save location discovery.

This class provides helper methods used by the UI export logic to build safe filenames for saving and to determine the preferred location for exported files. Use sanitizeFilename(String) when creating user-generated filenames to avoid platform-specific issues.

  • Method Details

    • getPreferredDownloadsDir

      public static String getPreferredDownloadsDir()
      Gets the user's Downloads directory if it exists, otherwise returns the system temp directory.
      Returns:
      the preferred downloads directory path
    • sanitizeFilename

      public static String sanitizeFilename(String input)
      Sanitizes a string to be safe for use as a filename. Replaces non-alphanumeric characters (except . - _) with underscores, collapses multiple underscores, and trims leading/trailing underscores/dots.
      Parameters:
      input - the input string
      Returns:
      the sanitized filename
    • getDefaultFilename

      public static String getDefaultFilename(String title, String extension)
      Generates a default filename for saving using the title or a timestamp.
      Parameters:
      title - the title text (may be empty or null)
      extension - the file extension without a leading dot (e.g., "json")
      Returns:
      the full path to the default filename
    • ensureExtension

      public static String ensureExtension(String filename, String extension)
      Ensures a filename has the specified extension. If it doesn't already end with the extension, it will be appended.
      Parameters:
      filename - the filename
      extension - the desired extension without a leading dot
      Returns:
      the filename with the ensured extension
    • getExtension

      public static String getExtension(String filename)
      Gets the file extension from a filename.
      Parameters:
      filename - the filename
      Returns:
      the extension without the leading dot, or empty string if no extension
    • getTimestampForFilename

      public static String getTimestampForFilename()
      Convenience method that returns the current system timestamp formatted for use as a filename suffix. This can be used when generating default filenames for exports.
      Returns:
      formatted timestamp like "2025-11-18_143020"