类 DiskUtils
- java.lang.Object
-
- com.alibaba.nacos.sys.utils.DiskUtils
-
public final class DiskUtils extends java.lang.ObjectIO operates on the utility class.- 作者:
- liaochuntao
-
-
嵌套类概要
嵌套类 修饰符和类型 类 说明 static classDiskUtils.LineIterator
-
字段概要
字段 修饰符和类型 字段 说明 private static java.nio.charset.CharsetCHARSETprivate static java.nio.charset.CharsetDecoderDECODERprivate static java.lang.StringDISK_QUATA_CNprivate static java.lang.StringDISK_QUATA_ENprivate static org.slf4j.LoggerLOGGERprivate static java.lang.StringNO_SPACE_CNprivate static java.lang.StringNO_SPACE_EN
-
构造器概要
构造器 构造器 说明 DiskUtils()
-
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static voidcompress(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 voidcompressDirectoryToZipFile(java.lang.String rootDir, java.lang.String sourceDir, java.util.zip.ZipOutputStream zos)static voidcompressIntoZipFile(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 voidcompressIntoZipFile(java.lang.String childName, java.io.InputStream inputStream, java.util.zip.ZipOutputStream zipOutputStream)static voidcopyDirectory(java.io.File srcDir, java.io.File destDir)static voidcopyFile(java.io.File src, java.io.File target)static java.io.FilecreateTmpFile(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.FilecreateTmpFile(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 voiddecompress(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 voiddeleteDirectory(java.lang.String path)static voiddeleteDirThenMkdir(java.lang.String path)static booleandeleteFile(java.lang.String path, java.lang.String fileName)delete target file.static voiddeleteQuietly(java.io.File file)static voiddeleteQuietly(java.nio.file.Path path)static voidforceMkdir(java.io.File file)static voidforceMkdir(java.lang.String path)static DiskUtils.LineIteratorlineIterator(java.io.File file)Returns an Iterator for the lines in aFileusing the default encoding for the VM.static DiskUtils.LineIteratorlineIterator(java.io.File file, java.lang.String encoding)Returns an Iterator for the lines in aFile.static java.io.FileopenFile(java.lang.String path, java.lang.String fileName)static java.io.FileopenFile(java.lang.String path, java.lang.String fileName, boolean rewrite)open file.static java.lang.StringreadFile(java.io.File file)read this file content.static java.lang.StringreadFile(java.io.InputStream is)read file content byInputStream.static java.lang.StringreadFile(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 voidtouch(java.io.File file)Implements the same behaviour as the "touch" utility on Unix.static voidtouch(java.lang.String path, java.lang.String fileName)static booleanwriteFile(java.io.File file, byte[] content, boolean append)Writes the contents to the target file.
-
-
-
字段详细资料
-
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
-
-
方法详细资料
-
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.IOExceptionImplements 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.IOExceptionCreates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. The resultingPathis associated with the sameFileSystemas 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
prefixandsuffixare used to construct candidate names in the same manner as theFile.createTempFile(String, String, File)method.- 参数:
dir- the path to directory in which to create the fileprefix- the prefix string to be used in generating the file's name; may benullsuffix- the suffix string to be used in generating the file's name; may benull, 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 namejava.lang.UnsupportedOperationException- if the array contains an attribute that cannot be set atomically when creating the directoryjava.io.IOException- if an I/O error occurs ordirdoes not existjava.lang.SecurityException- In the case of the default provider, and a security manager is installed, thecheckWritemethod 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.IOExceptionCreates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. The resultingPathis associated with the defaultFileSystem.- 参数:
prefix- the prefix string to be used in generating the file's name; may benullsuffix- the suffix string to be used in generating the file's name; may benull, 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 namejava.lang.UnsupportedOperationException- if the array contains an attribute that cannot be set atomically when creating the directoryjava.io.IOException- if an I/O error occurs or the temporary-file directory does not existjava.lang.SecurityException- In the case of the default provider, and a security manager is installed, thecheckWritemethod 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- directoryfileName- filename- 返回:
- content
-
readFile
public static java.lang.String readFile(java.io.InputStream is)
read file content byInputStream.- 参数:
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 filecontent- contentappend- 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- directoryfileName- 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- directoryfileName- filenamerewrite- 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.IOExceptionCompress a folder in a directory.- 参数:
rootDir- directorysourceDir- folderoutputFile- output filechecksum- 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.IOExceptionCompress an input stream to zip file.- 参数:
childName- child name in zip fileinputStream- input stream needed compressoutputFile- output filechecksum- 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.IOExceptionUnzip the target file to the specified folder.- 参数:
sourceFile- target fileoutputDir- specified folderchecksum- checksum- 抛出:
java.io.IOException- IOException
-
decompress
public static byte[] decompress(java.lang.String sourceFile, java.util.zip.Checksum checksum) throws java.io.IOExceptionUnzip the target file to byte array.- 参数:
sourceFile- target filechecksum- 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 aFile.This method opens an
The recommended usage pattern is:InputStreamfor the file. When you have finished with the iterator you should close the stream to free internal resources. This can be done by calling theLineIterator.close()orLineIterator.closeQuietly(org.apache.commons.io.LineIterator)method.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 benullencoding- the encoding to use,nullmeans 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 aFileusing the default encoding for the VM.- 参数:
file- the file to open for input, must not benull- 返回:
- 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)
-
-