Class DiskUtils
- Author:
- liaochuntao
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidCompress a folder in a directory.static voidcompressIntoZipFile(String childName, InputStream inputStream, String outputFile, Checksum checksum) Compress an input stream to zip file.static voidcopyDirectory(File srcDir, File destDir) static voidstatic FilecreateTmpFile(String prefix, String suffix) Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.static FilecreateTmpFile(String dir, String prefix, String suffix) Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.static voiddecompress(String sourceFile, String outputDir, Checksum checksum) Unzip the target file to the specified folder.static byte[]decompress(String sourceFile, Checksum checksum) Unzip the target file to byte array.static voiddeleteDirectory(String path) static voiddeleteDirThenMkdir(String path) static booleandeleteFile(String path, String fileName) delete target file.static voiddeleteQuietly(File file) static voiddeleteQuietly(Path path) static voidforceMkdir(File file) static voidforceMkdir(String path) static booleanisIllegalFileName(String fileName) Whether is illegal file name, it should not be start with root path '/' or '\\' and should not contain top path...static booleanisIllegalPath(String path) static DiskUtils.LineIteratorlineIterator(File file) Returns an Iterator for the lines in aFileusing the default encoding for the VM.static DiskUtils.LineIteratorlineIterator(File file, String encoding) Returns an Iterator for the lines in aFile.static Filestatic Fileopen file.static Stringread this file content.static StringreadFile(InputStream is) read file content byInputStream.static Stringread file which under the path.static byte[]readFileBytes(File file) read this file content then return bytes.static byte[]readFileBytes(String path, String fileName) read this file content then return bytes.static voidImplements the same behaviour as the "touch" utility on Unix.static voidTouch file.static booleanWrites the contents to the target file.
-
Constructor Details
-
DiskUtils
public DiskUtils()
-
-
Method Details
-
touch
Touch file.- Parameters:
path- path of fileNamefileName- fileName- Throws:
IOException- during touch
-
touch
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
Creates 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.- Parameters:
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- 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 nameUnsupportedOperationException- if the array contains an attribute that cannot be set atomically when creating the directoryIOException- if an I/O error occurs ordirdoes not existSecurityException- In the case of the default provider, and a security manager is installed, thecheckWritemethod is invoked to check write access to the file.
-
createTmpFile
Creates 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.- Parameters:
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- 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 nameUnsupportedOperationException- if the array contains an attribute that cannot be set atomically when creating the directoryIOException- if an I/O error occurs or the temporary-file directory does not existSecurityException- In the case of the default provider, and a security manager is installed, thecheckWritemethod is invoked to check write access to the file.
-
readFile
read file which under the path.- Parameters:
path- directoryfileName- filename- Returns:
- content
-
readFile
read file content byInputStream.- Parameters:
is-InputStream- Returns:
- content
-
readFile
read this file content.- Parameters:
file-File- Returns:
- content
-
readFileBytes
read this file content then return bytes.- Parameters:
file-File- Returns:
- content bytes
-
readFileBytes
read this file content then return bytes.- Parameters:
path- path of filefileName- file name- Returns:
- content bytes
-
writeFile
Writes the contents to the target file.- Parameters:
file- target filecontent- contentappend- write append mode- Returns:
- write success
-
deleteQuietly
-
deleteQuietly
-
deleteFile
delete target file.- Parameters:
path- directoryfileName- filename- Returns:
- delete success
-
deleteDirectory
- Throws:
IOException
-
forceMkdir
- Throws:
IOException
-
forceMkdir
- Throws:
IOException
-
deleteDirThenMkdir
- Throws:
IOException
-
copyDirectory
- Throws:
IOException
-
copyFile
- Throws:
IOException
-
openFile
-
openFile
open file.- Parameters:
path- directoryfileName- filenamerewrite- 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- directorysourceDir- folderoutputFile- output filechecksum- 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 fileinputStream- input stream needed compressoutputFile- output filechecksum- 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 fileoutputDir- specified folderchecksum- checksum- Throws:
IOException- IOException
-
decompress
Unzip the target file to byte array.- Parameters:
sourceFile- target filechecksum- checksum- Returns:
- decompress byte array
- Throws:
IOException- IOException during decompress
-
isIllegalFileName
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:
truewhen file name contain..or start with root path.
-
isIllegalPath
-
lineIterator
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.
- Parameters:
file- the file to open for input, must not benullencoding- the encoding to use,nullmeans 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
Returns an Iterator for the lines in aFileusing the default encoding for the VM.- Parameters:
file- the file to open for input, must not benull- 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:
-