Skip to content

FileUtils

Fulminazzo edited this page Oct 2, 2023 · 1 revision

While coding projects, you will be many times working with files for creating, modifying and deleting them. BearCommands offers a utility class to easily accomplish these goals: FileUtils.
FileUtils contains all sorts of functions to manipulate files and the file system. We will go through each one of them, explaining the parameters and the result.
Do keep in mind that, except for compareTwoFiles(), all functions throw an IOException with the corresponding error, in case it occurs:

  • writeToFile(File output, InputStream inputStream): writes the contents of the given inputstream to the output file. If the file does not exist, it will be created;
  • createNewFile(File file): creates a new file. If the parent directory do not exist, they will be created using createFolder();
  • createFolder(File folder): creates a new folder. If the parent directory do not exist, they will be created using createFolder();
  • copyFile(File file1, File file2): copies one file contents into another. If the file does not exist, it will be created;
  • renameFile(File fileFrom, File fileTo): renames one file to another (also works for moving files).
  • deleteFile(File file): deletes the specified file;
  • deleteFolder(File folder): deletes the files and the folders contained in the folder, then deletes the directory itself;
  • compareTwoFiles(File file1, File file2): compares the content of two files line by line. Returns true only if they are identical.