Class FileUtils

java.lang.Object
org.apache.juneau.commons.utils.FileUtils

public class FileUtils extends Object
File utilities.
  • Method Details

    • create

      public static void create(File f)
      Creates a file if it doesn't already exist using File.createNewFile().

      Throws a RuntimeException if the file could not be created.

      Parameters:
      f - The file to create. Must not be null.
    • createTempFile

      public static File createTempFile(String name) throws IOException
      Create a temporary file with the specified name.

      The name is broken into file name and suffix, and the parts are passed to File.createTempFile(String, String).

      File.deleteOnExit() is called on the resulting file before being returned by this method.

      Parameters:
      name - The file name. Must not be null (unguarded — a null name throws NullPointerException).
      Returns:
      A newly-created temporary file.
      Throws:
      IOException - Thrown by underlying stream.
    • createTempFile

      public static File createTempFile(String name, String contents) throws IOException
      Create a temporary file with the specified name and specified contents.

      The name is broken into file name and suffix, and the parts are passed to File.createTempFile(String, String).

      File.deleteOnExit() is called on the resulting file before being returned by this method.

      Parameters:
      name - The file name. Must not be null (unguarded — a null name throws NullPointerException).
      contents - The file contents. Can be null (creates an empty file).
      Returns:
      A newly-created temporary file.
      Throws:
      IOException - Thrown by underlying stream.
    • deleteFile

      public static boolean deleteFile(File f)
      Recursively deletes a file or directory.
      Parameters:
      f - The file or directory to delete. Can be null (returns true).
      Returns:
      true if file or directory was successfully deleted.
    • fileExists

      public static boolean fileExists(File dir, String fileName)
      Returns true if the specified file exists in the specified directory.
      Parameters:
      dir - The directory. Can be null (returns false).
      fileName - The file name. Can be null (returns false).
      Returns:
      true if the specified file exists in the specified directory.
    • getBaseName

      public static String getBaseName(String name)
      Strips the extension from a file name.
      Parameters:
      name - The file name.
      Returns:
      The file name without the extension, or null if name was null.
    • getFileExtension

      public static String getFileExtension(String name)
      Returns the extension from a file name.
      Parameters:
      name - The file name.
      Returns:
      The the extension, or null if name was null.
    • getFileName

      public static String getFileName(String path)
      Given an arbitrary path, returns the file name portion of that path.
      Parameters:
      path - The path to check. Can be null or empty (returns null).
      Returns:
      The file name, or null if the path was null, empty, or contained only slashes.
    • hasExtension

      public static boolean hasExtension(String name, String ext)
      Returns true if the specified file name contains the specified extension.
      Parameters:
      name - The file name. Can be null (returns false).
      ext - The extension. Can be null (returns false).
      Returns:
      true if the specified file name contains the specified extension.
    • mkdirs

      public static File mkdirs(File f, boolean clean)
      Same as File.mkdirs() except throws a RuntimeExeption if directory could not be created.
      Parameters:
      f - The directory to create. Must not be null.
      clean - If true, deletes the contents of the directory if it already exists.
      Returns:
      The same file.
      Throws:
      RuntimeException - if directory could not be created.
    • mkdirs

      public static File mkdirs(String path, boolean clean)
      Same as mkdirs(String, boolean) but uses String path.
      Parameters:
      path - The path of the directory to create. Must not be null
      clean - If true, deletes the contents of the directory if it already exists.
      Returns:
      The directory.
    • resolveSafely

      public static Optional<File> resolveSafely(File rootDir, String userPath)
      Resolves a user-supplied path string against a root directory with a strict filesystem-boundary check — rejects any path that escapes the root (via ../, absolute-path injection, symlinks pointing outside, etc.).

      Resolves both rootDir and the resolved target via Path.toRealPath() + Path.normalize() to handle symlinks deterministically, then asserts target.startsWith(root) before returning the resolved File.

      Symlinks inside rootDir that resolve inside rootDir are followed silently. Symlinks that resolve outside rootDir are rejected with IllegalArgumentException (callers should map to a 403 Forbidden).

      Pre-existence semantics: when the resolved target does not exist on the filesystem, the helper returns Optional.empty() so the caller can map to a 404 Not Found. Boundary violation always wins over pre-existence — even a non-existent path that escapes rootDir (after ../ normalization) is rejected with IllegalArgumentException rather than Optional.empty().

      Used by DirectoryResource, LogsResource, and any other resource that resolves an HTTP-supplied path under a configured root directory.

      Parameters:
      rootDir - The configured root directory. Must not be null.
      userPath - The user-supplied path, relative to rootDir. May be null or empty — in which case the helper returns the resolved root itself.
      Returns:
      An Optional containing the resolved File if it exists inside rootDir, or Optional.empty() if the target does not exist.
      Throws:
      IllegalArgumentException - If the resolved target escapes rootDir, or if the supplied userPath is not a valid path string. Callers should map to 403 Forbidden.
    • resolveVirtualPathSafely

      public static String resolveVirtualPathSafely(String basePath, String userPath)
      Virtual-path variant of resolveSafely(File, String) for use with servlet-context paths, classpath resource lookups, or other virtual-path resolvers where no filesystem operation is desired.

      Operates purely on the virtual path string — no toRealPath() call, no filesystem touch, no symlink resolution. Safe to use against base paths whose resources do not exist on the local filesystem at all (e.g. /WEB-INF/views/ inside a Spring Boot fat jar, or a classpath URL).

      Normalizes the combined path by walking segments and applying ./ (drop) and ../ (pop) semantics, then asserts the result stays under the normalized basePath. ../ segments that would pop above basePath are rejected.

      URL-encoding is the caller's responsibility — this helper treats the literal %2e%2e as a normal path segment (not as ..). Callers that receive user input from URLs should URL-decode before calling.

      Parameters:
      basePath - The configured base path (virtual; e.g. "/WEB-INF/views/"). Must not be null or blank.
      userPath - The user-supplied path, relative to basePath. May be null or empty — in which case the helper returns the normalized basePath.
      Returns:
      The resolved virtual path string (always starting with basePath).
      Throws:
      IllegalArgumentException - If the resolved target escapes basePath, or if basePath is null or blank. Callers should map to 403 Forbidden.
    • modifyTimestamp

      public static void modifyTimestamp(File f)
      Updates the modified timestamp on the specified file.

      Method ensures that the timestamp changes even if it's been modified within the past millisecond.

      Parameters:
      f - The file to modify the modified timestamp on. Must not be null.