public final class DiskUtils extends Object
| 限定符和类型 | 类和说明 |
|---|---|
static class |
DiskUtils.LineIterator |
| 构造器和说明 |
|---|
DiskUtils() |
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
compress(String rootDir,
String sourceDir,
String outputFile,
Checksum checksum)
Compress a folder in a directory.
|
static void |
compressIntoZipFile(String childName,
InputStream inputStream,
String outputFile,
Checksum checksum)
Compress an input stream to zip file.
|
static void |
copyDirectory(File srcDir,
File destDir) |
static void |
copyFile(File src,
File target) |
static File |
createTmpFile(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 File |
createTmpFile(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 byte[] |
decompress(String sourceFile,
Checksum checksum)
Unzip the target file to byte array.
|
static void |
decompress(String sourceFile,
String outputDir,
Checksum checksum)
Unzip the target file to the specified folder.
|
static void |
deleteDirectory(String path) |
static void |
deleteDirThenMkdir(String path) |
static boolean |
deleteFile(String path,
String fileName)
delete target file.
|
static void |
deleteQuietly(File file) |
static void |
deleteQuietly(Path path) |
static void |
forceMkdir(File file) |
static void |
forceMkdir(String path) |
static DiskUtils.LineIterator |
lineIterator(File file)
Returns an Iterator for the lines in a
File using the default encoding for the VM. |
static DiskUtils.LineIterator |
lineIterator(File file,
String encoding)
Returns an Iterator for the lines in a
File. |
static File |
openFile(String path,
String fileName) |
static File |
openFile(String path,
String fileName,
boolean rewrite)
open file.
|
static String |
readFile(File file)
read this file content.
|
static String |
readFile(InputStream is)
read file content by
InputStream. |
static String |
readFile(String path,
String fileName)
read file which under the path.
|
static byte[] |
readFileBytes(File file)
read this file content then return bytes.
|
static byte[] |
readFileBytes(String path,
String fileName) |
static void |
touch(File file)
Implements the same behaviour as the "touch" utility on Unix.
|
static void |
touch(String path,
String fileName) |
static boolean |
writeFile(File file,
byte[] content,
boolean append)
Writes the contents to the target file.
|
public static void touch(String path, String fileName) throws IOException
IOExceptionpublic static void touch(File file) throws IOException
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 touchIOException - If an I/O problem occurspublic static File createTmpFile(String dir, String prefix, String suffix) throws IOException
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 fileprefix - the prefix string to be used in generating the file's name; may be nullsuffix - the suffix string to be used in generating the file's name; may be null, in which case
".tmp" is usedIllegalArgumentException - 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 dir does not existSecurityException - 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.public static File createTmpFile(String prefix, String suffix) throws IOException
Path is associated with the default FileSystem.prefix - the prefix string to be used in generating the file's name; may be nullsuffix - the suffix string to be used in generating the file's name; may be null, in which case
".tmp" is usedIllegalArgumentException - 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,
the checkWrite method is invoked
to check write access to the file.public static String readFile(String path, String fileName)
path - directoryfileName - filenamepublic static String readFile(InputStream is)
InputStream.is - InputStreampublic static String readFile(File file)
file - Filepublic static byte[] readFileBytes(File file)
file - Filepublic static boolean writeFile(File file, byte[] content, boolean append)
file - target filecontent - contentappend - write append modepublic static void deleteQuietly(File file)
public static void deleteQuietly(Path path)
public static boolean deleteFile(String path, String fileName)
path - directoryfileName - filenamepublic static void deleteDirectory(String path) throws IOException
IOExceptionpublic static void forceMkdir(String path) throws IOException
IOExceptionpublic static void forceMkdir(File file) throws IOException
IOExceptionpublic static void deleteDirThenMkdir(String path) throws IOException
IOExceptionpublic static void copyDirectory(File srcDir, File destDir) throws IOException
IOExceptionpublic static void copyFile(File src, File target) throws IOException
IOExceptionpublic static File openFile(String path, String fileName, boolean rewrite)
path - directoryfileName - filenamerewrite - if rewrite is true, will delete old file and create new oneFilepublic static void compress(String rootDir, String sourceDir, String outputFile, Checksum checksum) throws IOException
rootDir - directorysourceDir - folderoutputFile - output filechecksum - checksumIOException - IOExceptionpublic static void compressIntoZipFile(String childName, InputStream inputStream, String outputFile, Checksum checksum) throws IOException
childName - child name in zip fileinputStream - input stream needed compressoutputFile - output filechecksum - check sumIOException - IOException during compresspublic static void decompress(String sourceFile, String outputDir, Checksum checksum) throws IOException
sourceFile - target fileoutputDir - specified folderchecksum - checksumIOException - IOExceptionpublic static byte[] decompress(String sourceFile, Checksum checksum) throws IOException
sourceFile - target filechecksum - checksumIOException - IOException during decompresspublic static DiskUtils.LineIterator lineIterator(File file, String encoding) throws IOException
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.
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 nullencoding - the encoding to use, null means platform defaultnullIOException - in case of an I/O error (file closed)public static DiskUtils.LineIterator lineIterator(File file) throws IOException
File using the default encoding for the VM.file - the file to open for input, must not be nullnullIOException - in case of an I/O error (file closed)lineIterator(File, String)Copyright © 2018–2023 Alibaba Group. All rights reserved.