类 DiskUtils


  • public final class DiskUtils
    extends java.lang.Object
    IO operates on the utility class.
    作者:
    liaochuntao
    • 字段概要

      字段 
      修饰符和类型 字段 说明
      private static java.nio.charset.Charset CHARSET  
      private static java.nio.charset.CharsetDecoder DECODER  
      private static java.lang.String DISK_QUATA_CN  
      private static java.lang.String DISK_QUATA_EN  
      private static org.slf4j.Logger LOGGER  
      private static java.lang.String NO_SPACE_CN  
      private static java.lang.String NO_SPACE_EN  
    • 构造器概要

      构造器 
      构造器 说明
      DiskUtils()  
    • 方法概要

      所有方法 静态方法 具体方法 
      修饰符和类型 方法 说明
      static void compress​(java.lang.String rootDir, java.lang.String sourceDir, java.lang.String outputFile, java.util.zip.Checksum checksum)
      Compress a folder in a directory.
      private static void compressDirectoryToZipFile​(java.lang.String rootDir, java.lang.String sourceDir, java.util.zip.ZipOutputStream zos)  
      static void compressIntoZipFile​(java.lang.String childName, java.io.InputStream inputStream, java.lang.String outputFile, java.util.zip.Checksum checksum)
      Compress an input stream to zip file.
      private static void compressIntoZipFile​(java.lang.String childName, java.io.InputStream inputStream, java.util.zip.ZipOutputStream zipOutputStream)  
      static void copyDirectory​(java.io.File srcDir, java.io.File destDir)  
      static void copyFile​(java.io.File src, java.io.File target)  
      static java.io.File createTmpFile​(java.lang.String prefix, java.lang.String suffix)
      Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
      static java.io.File createTmpFile​(java.lang.String dir, java.lang.String prefix, java.lang.String suffix)
      Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.
      static void decompress​(java.lang.String sourceFile, java.lang.String outputDir, java.util.zip.Checksum checksum)
      Unzip the target file to the specified folder.
      static byte[] decompress​(java.lang.String sourceFile, java.util.zip.Checksum checksum)
      Unzip the target file to byte array.
      static void deleteDirectory​(java.lang.String path)  
      static void deleteDirThenMkdir​(java.lang.String path)  
      static boolean deleteFile​(java.lang.String path, java.lang.String fileName)
      delete target file.
      static void deleteQuietly​(java.io.File file)  
      static void deleteQuietly​(java.nio.file.Path path)  
      static void forceMkdir​(java.io.File file)  
      static void forceMkdir​(java.lang.String path)  
      static DiskUtils.LineIterator lineIterator​(java.io.File file)
      Returns an Iterator for the lines in a File using the default encoding for the VM.
      static DiskUtils.LineIterator lineIterator​(java.io.File file, java.lang.String encoding)
      Returns an Iterator for the lines in a File.
      static java.io.File openFile​(java.lang.String path, java.lang.String fileName)  
      static java.io.File openFile​(java.lang.String path, java.lang.String fileName, boolean rewrite)
      open file.
      static java.lang.String readFile​(java.io.File file)
      read this file content.
      static java.lang.String readFile​(java.io.InputStream is)
      read file content by InputStream.
      static java.lang.String readFile​(java.lang.String path, java.lang.String fileName)
      read file which under the path.
      static byte[] readFileBytes​(java.io.File file)
      read this file content then return bytes.
      static byte[] readFileBytes​(java.lang.String path, java.lang.String fileName)  
      static void touch​(java.io.File file)
      Implements the same behaviour as the "touch" utility on Unix.
      static void touch​(java.lang.String path, java.lang.String fileName)  
      static boolean writeFile​(java.io.File file, byte[] content, boolean append)
      Writes the contents to the target file.
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 字段详细资料

      • LOGGER

        private static final org.slf4j.Logger LOGGER
      • NO_SPACE_CN

        private static final java.lang.String NO_SPACE_CN
        另请参阅:
        常量字段值
      • NO_SPACE_EN

        private static final java.lang.String NO_SPACE_EN
        另请参阅:
        常量字段值
      • DISK_QUATA_CN

        private static final java.lang.String DISK_QUATA_CN
        另请参阅:
        常量字段值
      • DISK_QUATA_EN

        private static final java.lang.String DISK_QUATA_EN
        另请参阅:
        常量字段值
      • CHARSET

        private static final java.nio.charset.Charset CHARSET
      • DECODER

        private static final java.nio.charset.CharsetDecoder DECODER
    • 构造器详细资料

      • DiskUtils

        public DiskUtils()
    • 方法详细资料

      • touch

        public static void touch​(java.lang.String path,
                                 java.lang.String fileName)
                          throws java.io.IOException
        抛出:
        java.io.IOException
      • touch

        public static void touch​(java.io.File file)
                          throws java.io.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.

        参数:
        file - the File to touch
        抛出:
        java.io.IOException - If an I/O problem occurs
      • createTmpFile

        public static java.io.File createTmpFile​(java.lang.String dir,
                                                 java.lang.String prefix,
                                                 java.lang.String suffix)
                                          throws java.io.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.

        参数:
        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
        返回:
        the path to the newly created file that did not exist before this method was invoked
        抛出:
        java.lang.IllegalArgumentException - if the prefix or suffix parameters cannot be used to generate a candidate file name
        java.lang.UnsupportedOperationException - if the array contains an attribute that cannot be set atomically when creating the directory
        java.io.IOException - if an I/O error occurs or dir does not exist
        java.lang.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 java.io.File createTmpFile​(java.lang.String prefix,
                                                 java.lang.String suffix)
                                          throws java.io.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.
        参数:
        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
        返回:
        the path to the newly created file that did not exist before this method was invoked
        抛出:
        java.lang.IllegalArgumentException - if the prefix or suffix parameters cannot be used to generate a candidate file name
        java.lang.UnsupportedOperationException - if the array contains an attribute that cannot be set atomically when creating the directory
        java.io.IOException - if an I/O error occurs or the temporary-file directory does not exist
        java.lang.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 java.lang.String readFile​(java.lang.String path,
                                                java.lang.String fileName)
        read file which under the path.
        参数:
        path - directory
        fileName - filename
        返回:
        content
      • readFile

        public static java.lang.String readFile​(java.io.InputStream is)
        read file content by InputStream.
        参数:
        is - InputStream
        返回:
        content
      • readFile

        public static java.lang.String readFile​(java.io.File file)
        read this file content.
        参数:
        file - File
        返回:
        content
      • readFileBytes

        public static byte[] readFileBytes​(java.io.File file)
        read this file content then return bytes.
        参数:
        file - File
        返回:
        content bytes
      • readFileBytes

        public static byte[] readFileBytes​(java.lang.String path,
                                           java.lang.String fileName)
      • writeFile

        public static boolean writeFile​(java.io.File file,
                                        byte[] content,
                                        boolean append)
        Writes the contents to the target file.
        参数:
        file - target file
        content - content
        append - write append mode
        返回:
        write success
      • deleteQuietly

        public static void deleteQuietly​(java.io.File file)
      • deleteQuietly

        public static void deleteQuietly​(java.nio.file.Path path)
      • deleteFile

        public static boolean deleteFile​(java.lang.String path,
                                         java.lang.String fileName)
        delete target file.
        参数:
        path - directory
        fileName - filename
        返回:
        delete success
      • deleteDirectory

        public static void deleteDirectory​(java.lang.String path)
                                    throws java.io.IOException
        抛出:
        java.io.IOException
      • forceMkdir

        public static void forceMkdir​(java.lang.String path)
                               throws java.io.IOException
        抛出:
        java.io.IOException
      • forceMkdir

        public static void forceMkdir​(java.io.File file)
                               throws java.io.IOException
        抛出:
        java.io.IOException
      • deleteDirThenMkdir

        public static void deleteDirThenMkdir​(java.lang.String path)
                                       throws java.io.IOException
        抛出:
        java.io.IOException
      • copyDirectory

        public static void copyDirectory​(java.io.File srcDir,
                                         java.io.File destDir)
                                  throws java.io.IOException
        抛出:
        java.io.IOException
      • copyFile

        public static void copyFile​(java.io.File src,
                                    java.io.File target)
                             throws java.io.IOException
        抛出:
        java.io.IOException
      • openFile

        public static java.io.File openFile​(java.lang.String path,
                                            java.lang.String fileName)
      • openFile

        public static java.io.File openFile​(java.lang.String path,
                                            java.lang.String fileName,
                                            boolean rewrite)
        open file.
        参数:
        path - directory
        fileName - filename
        rewrite - if rewrite is true, will delete old file and create new one
        返回:
        File
      • compress

        public static void compress​(java.lang.String rootDir,
                                    java.lang.String sourceDir,
                                    java.lang.String outputFile,
                                    java.util.zip.Checksum checksum)
                             throws java.io.IOException
        Compress a folder in a directory.
        参数:
        rootDir - directory
        sourceDir - folder
        outputFile - output file
        checksum - checksum
        抛出:
        java.io.IOException - IOException
      • compressDirectoryToZipFile

        private static void compressDirectoryToZipFile​(java.lang.String rootDir,
                                                       java.lang.String sourceDir,
                                                       java.util.zip.ZipOutputStream zos)
                                                throws java.io.IOException
        抛出:
        java.io.IOException
      • compressIntoZipFile

        public static void compressIntoZipFile​(java.lang.String childName,
                                               java.io.InputStream inputStream,
                                               java.lang.String outputFile,
                                               java.util.zip.Checksum checksum)
                                        throws java.io.IOException
        Compress an input stream to zip file.
        参数:
        childName - child name in zip file
        inputStream - input stream needed compress
        outputFile - output file
        checksum - check sum
        抛出:
        java.io.IOException - IOException during compress
      • compressIntoZipFile

        private static void compressIntoZipFile​(java.lang.String childName,
                                                java.io.InputStream inputStream,
                                                java.util.zip.ZipOutputStream zipOutputStream)
                                         throws java.io.IOException
        抛出:
        java.io.IOException
      • decompress

        public static void decompress​(java.lang.String sourceFile,
                                      java.lang.String outputDir,
                                      java.util.zip.Checksum checksum)
                               throws java.io.IOException
        Unzip the target file to the specified folder.
        参数:
        sourceFile - target file
        outputDir - specified folder
        checksum - checksum
        抛出:
        java.io.IOException - IOException
      • decompress

        public static byte[] decompress​(java.lang.String sourceFile,
                                        java.util.zip.Checksum checksum)
                                 throws java.io.IOException
        Unzip the target file to byte array.
        参数:
        sourceFile - target file
        checksum - checksum
        返回:
        decompress byte array
        抛出:
        java.io.IOException - IOException during decompress
      • lineIterator

        public static DiskUtils.LineIterator lineIterator​(java.io.File file,
                                                          java.lang.String encoding)
                                                   throws java.io.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.

        参数:
        file - the file to open for input, must not be null
        encoding - the encoding to use, null means platform default
        返回:
        an Iterator of the lines in the file, never null
        抛出:
        java.io.IOException - in case of an I/O error (file closed)
        从以下版本开始:
        1.2
      • lineIterator

        public static DiskUtils.LineIterator lineIterator​(java.io.File file)
                                                   throws java.io.IOException
        Returns an Iterator for the lines in a File using the default encoding for the VM.
        参数:
        file - the file to open for input, must not be null
        返回:
        an Iterator of the lines in the file, never null
        抛出:
        java.io.IOException - in case of an I/O error (file closed)
        从以下版本开始:
        1.3
        另请参阅:
        lineIterator(File, String)