Package com.digitizer.core
Class FileUtils
java.lang.Object
com.digitizer.core.FileUtils
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 Summary
Modifier and TypeMethodDescriptionstatic StringensureExtension(String filename, String extension) Ensures a filename has the specified extension.static StringgetDefaultFilename(String title, String extension) Generates a default filename for saving using the title or a timestamp.static StringgetExtension(String filename) Gets the file extension from a filename.static StringGets the user's Downloads directory if it exists, otherwise returns the system temp directory.static StringConvenience method that returns the current system timestamp formatted for use as a filename suffix.static StringsanitizeFilename(String input) Sanitizes a string to be safe for use as a filename.
-
Method Details
-
getPreferredDownloadsDir
Gets the user's Downloads directory if it exists, otherwise returns the system temp directory.- Returns:
- the preferred downloads directory path
-
sanitizeFilename
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
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
Ensures a filename has the specified extension. If it doesn't already end with the extension, it will be appended.- Parameters:
filename- the filenameextension- the desired extension without a leading dot- Returns:
- the filename with the ensured extension
-
getExtension
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
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"
-