Class DiskUtils

java.lang.Object
com.alibaba.nacos.sys.utils.DiskUtils

public final class DiskUtils extends Object
IO operates on the utility class.
Author:
liaochuntao
  • Constructor Details

    • DiskUtils

      public DiskUtils()
  • Method Details

    • touch

      public static void touch(String path, String fileName) throws IOException
      Touch file.
      Parameters:
      path - path of fileName
      fileName - fileName
      Throws:
      IOException - during touch
    • touch

      public static void touch(File file) throws IOException
      Implements the same behaviour as the "touch" utility on Unix. It creates a new file with size 0 or, if the file exists already, it is opened and closed without modifying it, but updating the file date and time.

      NOTE: As from v1.3, this method throws an IOException if the last modified date of the file cannot be set. Also, as from v1.3 this method creates parent directories if they do not exist.

      Parameters:
      file - the File to touch
      Throws:
      IOException - If an I/O problem occurs
    • createTmpFile

      public static File createTmpFile(String dir, String prefix, String suffix) throws IOException
      Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. The resulting Path is associated with the same FileSystem as the given directory.

      The details as to how the name of the file is constructed is implementation dependent and therefore not specified. Where possible the prefix and suffix are used to construct candidate names in the same manner as the File.createTempFile(String, String, File) method.

      Parameters:
      dir - the path to directory in which to create the file
      prefix - the prefix string to be used in generating the file's name; may be null
      suffix - the suffix string to be used in generating the file's name; may be null, in which case ".tmp" is used
      Returns:
      the path to the newly created file that did not exist before this method was invoked
      Throws:
      IllegalArgumentException - if the prefix or suffix parameters cannot be used to generate a candidate file name
      UnsupportedOperationException - if the array contains an attribute that cannot be set atomically when creating the directory
      IOException - if an I/O error occurs or dir does not exist
      SecurityException - In the case of the default provider, and a security manager is installed, the checkWrite method is invoked to check write access to the file.
    • createTmpFile

      public static File createTmpFile(String prefix, String suffix) throws IOException
      Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. The resulting Path is associated with the default FileSystem.
      Parameters:
      prefix - the prefix string to be used in generating the file's name; may be null
      suffix - the suffix string to be used in generating the file's name; may be null, in which case ".tmp" is used
      Returns:
      the path to the newly created file that did not exist before this method was invoked
      Throws:
      IllegalArgumentException - if the prefix or suffix parameters cannot be used to generate a candidate file name
      UnsupportedOperationException - if the array contains an attribute that cannot be set atomically when creating the directory
      IOException - if an I/O error occurs or the temporary-file directory does not exist
      SecurityException - In the case of the default provider, and a security manager is installed, the checkWrite method is invoked to check write access to the file.
    • readFile

      public static String readFile(String path, String fileName)
      read file which under the path.
      Parameters:
      path - directory
      fileName - filename
      Returns:
      content
    • readFile

      public static String readFile(InputStream is)
      read file content by InputStream.
      Parameters:
      is - InputStream
      Returns:
      content
    • readFile

      public static String readFile(File file)
      read this file content.
      Parameters:
      file - File
      Returns:
      content
    • readFileBytes

      public static byte[] readFileBytes(File file)
      read this file content then return bytes.
      Parameters:
      file - File
      Returns:
      content bytes
    • readFileBytes

      public static byte[] readFileBytes(String path, String fileName)
      read this file content then return bytes.
      Parameters:
      path - path of file
      fileName - file name
      Returns:
      content bytes
    • writeFile

      public static boolean writeFile(File file, byte[] content, boolean append)
      Writes the contents to the target file.
      Parameters:
      file - target file
      content - content
      append - write append mode
      Returns:
      write success
    • deleteQuietly

      public static void deleteQuietly(File file)
    • deleteQuietly

      public static void deleteQuietly(Path path)
    • deleteFile

      public static boolean deleteFile(String path, String fileName)
      delete target file.
      Parameters:
      path - directory
      fileName - filename
      Returns:
      delete success
    • deleteDirectory

      public static void deleteDirectory(String path) throws IOException
      Throws:
      IOException
    • forceMkdir

      public static void forceMkdir(String path) throws IOException
      Throws:
      IOException
    • forceMkdir

      public static void forceMkdir(File file) throws IOException
      Throws:
      IOException
    • deleteDirThenMkdir

      public static void deleteDirThenMkdir(String path) throws IOException
      Throws:
      IOException
    • copyDirectory

      public static void copyDirectory(File srcDir, File destDir) throws IOException
      Throws:
      IOException
    • copyFile

      public static void copyFile(File src, File target) throws IOException
      Throws:
      IOException
    • openFile

      public static File openFile(String path, String fileName)
    • openFile

      public static File openFile(String path, String fileName, boolean rewrite)
      open file.
      Parameters:
      path - directory
      fileName - filename
      rewrite - if rewrite is true, will delete old file and create new one
      Returns:
      File
    • compress

      public static void compress(String rootDir, String sourceDir, String outputFile, Checksum checksum) throws IOException
      Compress a folder in a directory.
      Parameters:
      rootDir - directory
      sourceDir - folder
      outputFile - output file
      checksum - checksum
      Throws:
      IOException - IOException
    • compressIntoZipFile

      public static void compressIntoZipFile(String childName, InputStream inputStream, String outputFile, Checksum checksum) throws IOException
      Compress an input stream to zip file.
      Parameters:
      childName - child name in zip file
      inputStream - input stream needed compress
      outputFile - output file
      checksum - check sum
      Throws:
      IOException - IOException during compress
    • decompress

      public static void decompress(String sourceFile, String outputDir, Checksum checksum) throws IOException
      Unzip the target file to the specified folder.
      Parameters:
      sourceFile - target file
      outputDir - specified folder
      checksum - checksum
      Throws:
      IOException - IOException
    • decompress

      public static byte[] decompress(String sourceFile, Checksum checksum) throws IOException
      Unzip the target file to byte array.
      Parameters:
      sourceFile - target file
      checksum - checksum
      Returns:
      decompress byte array
      Throws:
      IOException - IOException during decompress
    • isIllegalFileName

      public static boolean isIllegalFileName(String fileName)
      Whether is illegal file name, it should not be start with root path '/' or '\\' and should not contain top path ...
      Parameters:
      fileName - File name
      Returns:
      true when file name contain .. or start with root path.
    • isIllegalPath

      public static boolean isIllegalPath(String path)
    • lineIterator

      public static DiskUtils.LineIterator lineIterator(File file, String encoding) throws IOException
      Returns an Iterator for the lines in a File.

      This method opens an InputStream for the file. When you have finished with the iterator you should close the stream to free internal resources. This can be done by calling the LineIterator.close() or LineIterator.closeQuietly(org.apache.commons.io.LineIterator) method.

      The recommended usage pattern is:
       LineIterator it = FileUtils.lineIterator(file, "UTF-8");
       try {
         while (it.hasNext()) {
           String line = it.nextLine();
           /// do something with line
         }
       } finally {
         LineIterator.closeQuietly(iterator);
       }
       

      If an exception occurs during the creation of the iterator, the underlying stream is closed.

      Parameters:
      file - the file to open for input, must not be null
      encoding - the encoding to use, null means platform default
      Returns:
      an Iterator of the lines in the file, never null
      Throws:
      IOException - in case of an I/O error (file closed)
      Since:
      1.2
    • lineIterator

      public static DiskUtils.LineIterator lineIterator(File file) throws IOException
      Returns an Iterator for the lines in a File using the default encoding for the VM.
      Parameters:
      file - the file to open for input, must not be null
      Returns:
      an Iterator of the lines in the file, never null
      Throws:
      IOException - in case of an I/O error (file closed)
      Since:
      1.3
      See Also: