Class FileUtils

java.lang.Object
com.alibaba.cloud.commons.io.FileUtils

public final class FileUtils extends Object
FileUtils. copy from apache commons-io.
Author:
theonefx
  • Method Details

    • openInputStream

      public static FileInputStream openInputStream(File file) throws IOException
      Opens a FileInputStream for the specified file, providing better error messages than simply calling new FileInputStream(file).

      At the end of the method either the stream will be successfully opened, or an exception will have been thrown.

      An exception is thrown if the file does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be read.

      Parameters:
      file - the file to open for input, must not be null
      Returns:
      a new FileInputStream for the specified file
      Throws:
      FileNotFoundException - if the file does not exist
      IOException - if the file object is a directory
      IOException - if the file cannot be read
      Since:
      1.3
    • readFileToString

      public static String readFileToString(File file, Charset encoding) throws IOException
      Reads the contents of a file into a String. The file is always closed.
      Parameters:
      file - the file to read, must not be null
      encoding - the encoding to use, null means platform default
      Returns:
      the file contents, never null
      Throws:
      IOException - in case of an I/O error
    • readFileToString

      public static String readFileToString(File file, String encoding) throws IOException
      Reads the contents of a file into a String. The file is always closed.
      Parameters:
      file - the file to read, must not be null
      encoding - the encoding to use, null means platform default
      Returns:
      the file contents, never null
      Throws:
      IOException - in case of an I/O error
      UnsupportedCharsetException - thrown instead of .UnsupportedEncodingException in version 2.2 if the encoding is not supported.
    • readFileToString

      @Deprecated public static String readFileToString(File file) throws IOException
      Deprecated.
      2.5 use readFileToString(File, String) instead (and specify the appropriate encoding)
      Reads the contents of a file into a String using the default encoding for the VM. The file is always closed.
      Parameters:
      file - the file to read, must not be null
      Returns:
      the file contents, never null
      Throws:
      IOException - in case of an I/O error