Class FileIOUtils

java.lang.Object
com.vaadin.flow.internal.FileIOUtils

public class FileIOUtils extends Object
Utility class for file I/O operations, including conditional file writing, file searching, and content comparison.

For internal use only. May be renamed or removed in a future release.

Since:
25.0
  • Method Details

    • deleteFileQuietly

      @Deprecated(since="25.3", forRemoval=true) public static boolean deleteFileQuietly(File file)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use deleteQuietly(File) instead, which also deletes directory contents and logs why a deletion failed instead of failing silently
      Deletes file if it exists and eats exceptions. Note, this is an internal helper method, use only from framework code.
      Parameters:
      file - to be deleted
      Returns:
      true if succeeded
    • deleteQuietly

      public static boolean deleteQuietly(File file)
      Deletes a file, or a directory and its contents, logging a warning that explains how to unblock the deletion if it fails. Note, this is an internal helper method, use only from framework code.
      Parameters:
      file - the file or directory to delete, may be null
      Returns:
      true if the file was deleted or did not exist
    • deleteQuietly

      public static boolean deleteQuietly(Path path)
      Deletes a file, or a directory and its contents, logging a warning that explains how to unblock the deletion if it fails. Note, this is an internal helper method, use only from framework code.
      Parameters:
      path - the file or directory to delete, may be null
      Returns:
      true if the file was deleted or did not exist
    • urlToString

      public static String urlToString(URL url) throws IOException
      Reads the content from given URL into UTF 8 String. Note, this is an internal helper method, use only from framework code.
      Parameters:
      url - the URL to read
      Returns:
      string from the content
      Throws:
      IOException
    • copyDirectory

      public static void copyDirectory(File source, File target) throws IOException
      Copies a directory recursively.
      Parameters:
      source - the source directory
      target - the target directory
      Throws:
      IOException - if an I/O error occurs
    • copyDirectory

      public static void copyDirectory(File source, File target, FileFilter filter) throws IOException
      Copies a directory recursively with a file filter.
      Parameters:
      source - the source directory
      target - the target directory
      filter - the file filter to apply, or null to copy all files
      Throws:
      IOException - if an I/O error occurs
    • delete

      public static void delete(File file) throws IOException
      Deletes a file, or a directory and its contents, recursively. Does nothing if the file does not exist.

      Symbolic links and Windows junctions are removed as links, the contents they point to are left untouched.

      Parameters:
      file - the file or directory to delete
      Throws:
      IOException - if the file or any of its contents could not be deleted, with a message describing how the deletion can be unblocked
    • delete

      public static void delete(Path path) throws IOException
      Deletes a file, or a directory and its contents, recursively. Does nothing if the file does not exist.

      Symbolic links and Windows junctions are removed as links, the contents they point to are left untouched.

      Parameters:
      path - the file or directory to delete
      Throws:
      IOException - if the file or any of its contents could not be deleted, with a message describing how the deletion can be unblocked
    • getUserDirectory

      public static File getUserDirectory()
      Gets the user's home directory.
      Returns:
      the user's home directory
    • listFiles

      public static List<File> listFiles(File directory, String[] extensions, boolean recursive) throws IOException
      Lists all files in a directory with the specified extensions, recursively.
      Parameters:
      directory - the directory to search
      extensions - the file extensions to include (without dots)
      recursive - whether to search recursively
      Returns:
      a list of files matching the criteria
      Throws:
      IOException - if an I/O error occurs
    • contentEquals

      public static boolean contentEquals(InputStream input1, InputStream input2) throws IOException
      Compares the content of two InputStreams.
      Parameters:
      input1 - the first InputStream
      input2 - the second InputStream
      Returns:
      true if the content is equal, false otherwise
      Throws:
      IOException - if an I/O error occurs
    • closeQuietly

      public static void closeQuietly(AutoCloseable closeable)
      Closes a resource quietly without throwing an exception.
      Parameters:
      closeable - the resource to close
    • removeExtension

      public static String removeExtension(String filename)
      Removes the extension from a filename.
      Parameters:
      filename - the filename
      Returns:
      the filename without extension
    • wildcardMatch

      public static boolean wildcardMatch(String text, String pattern)
      Checks if a string matches a wildcard pattern.
      Parameters:
      text - the text to check
      pattern - the wildcard pattern (* and ? are supported)
      Returns:
      true if the text matches the pattern, false otherwise
    • isEmptyDirectory

      public static boolean isEmptyDirectory(Path directory) throws IOException
      Checks if a directory is empty.
      Parameters:
      directory - the directory to check
      Returns:
      true if the directory is empty, false otherwise
      Throws:
      IOException - if an I/O error occurs
    • writeIfChanged

      public static boolean writeIfChanged(File file, List<String> content) throws IOException
      Writes the given content into the given file unless the file already contains that content.
      Parameters:
      file - the file to write to
      content - the lines to write
      Returns:
      true if the content was written to the file, false otherwise
      Throws:
      IOException - if something went wrong
    • writeIfChanged

      public static boolean writeIfChanged(File file, String content) throws IOException
      Writes the given content into the given file unless the file already contains that content.
      Parameters:
      file - the file to write to
      content - the content to write
      Returns:
      true if the content was written to the file, false otherwise
      Throws:
      IOException - if something went wrong
    • getProjectFolderFromClasspath

      public static File getProjectFolderFromClasspath()
      Try determining the project folder from the classpath.
      Returns:
      A file referring to the project folder or null if the folder could not be determined
    • isProbablyTemporaryFile

      public static boolean isProbablyTemporaryFile(File file)
      Checks if the given file is likely a temporary file created by an editor.
      Parameters:
      file - the file to check
      Returns:
      true if the file is likely a temporary file, false otherwise
    • getFilesByPattern

      public static List<Path> getFilesByPattern(Path baseDir, String pattern) throws IOException
      Get a list of files in a given directory that match a given glob pattern.
      Parameters:
      baseDir - a directory to walk in
      pattern - glob pattern to filter files, e.g. "*.js".
      Returns:
      a list of files matching a given pattern
      Throws:
      IOException - if an I/O error is thrown while walking through the tree in base directory
    • compareIgnoringIndentationAndEOL

      public static boolean compareIgnoringIndentationAndEOL(String content1, String content2, BiPredicate<String,String> compareFn)
      Compare two file content strings ignoring indentation and EOL characters.
      Parameters:
      content1 - the first file content to compare
      content2 - the second file content to compare
      compareFn - a function to compare the normalized strings
      Returns:
      true if the normalized strings are equal, false otherwise
    • compareIgnoringIndentationEOLAndWhiteSpace

      public static boolean compareIgnoringIndentationEOLAndWhiteSpace(String content1, String content2, BiPredicate<String,String> compareFn)
      Compare two file content strings ignoring indentation, EOL characters and white space where it does not matter (before and after {, }, ' and : chars).
      Parameters:
      content1 - the first file content to compare
      content2 - the second file content to compare
      compareFn - a function to compare the normalized strings
      Returns:
      true if the normalized strings are equal, false otherwise