public class FileUtil extends Object
Constructor and Description |
---|
FileUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
appendFileBytes(File file,
byte[] bytes)
Appends the entire contents of the given byte array to a file.
|
static Socket |
close(Socket socket)
Closes the given socket if not null
|
static <T extends Closeable> |
close(T close)
Closes the given stream if not null
|
static <T extends InputStream> |
close(T is)
Closes the given stream if not null
|
static <T extends OutputStream> |
close(T os)
Closes the given stream if not null
|
static void |
closeQuietly(Closeable close)
Closes the input stream if not null, suppressing any exception.
|
static void |
closeQuietly(InputStream is)
Closes the input stream if not null, suppressing any exception.
|
static void |
closeQuietly(OutputStream os)
Closes the output stream if not null, suppressing any exception.
|
static void |
closeQuietly(Reader is)
Closes the input stream if not null, suppressing any exception.
|
static void |
closeQuietly(Socket socket)
Closes the socket if not null, suppressing any exception.
|
static void |
copy(InputStream in,
OutputStream out)
Copies the contents of the given input stream to the given output stream.
|
static void |
copyFile(File from,
File to)
Copies the file to the given location.
|
static void |
copyRecursively(File from,
File to)
Recursively copies the file/directory to the given file/directory.
|
static int |
countLines(String outputFile) |
static boolean |
deleteDirectory(File directory,
boolean removeRoot)
Recursively deletes a directory structure starting at specified root directory.
|
static boolean |
deleteDirectory(String dirPath,
boolean removeRoot)
Recursively deletes a directory structure starting at specified root directory.
|
static void |
deleteRecursively(File file)
Recursively deletes the file and all subdirectories.
|
static void |
deleteRecursivelyWithFailure(File dir)
Deletes the given directory or file, throwing an exception of unable to delete
|
static File[] |
listFilesCanonical(File directory)
Lists the files in the given directory in a canonical ordering (one that is not subject to platform-specific
ordering).
|
static Properties |
loadPropertiesFile(File file)
Loads the given properties file.
|
static String |
readData(File file)
Read data from a file.
|
static byte[] |
readFileBytes(File file)
Reads the entire contents of the given file into a byte array.
|
static String |
readFileString(File file,
Charset charset)
Reads the entire contents of the given file into a string.
|
static byte[] |
readZipEntry(File zipFile,
String entry)
Reads an entry from a zip file
|
static void |
savePropertiesFile(File file,
Properties properties)
Saves the given properties object.
|
static void |
unzip(File from,
File to)
Recursively unzips the given file to a given directory/file
|
static void |
writeData(String data,
String fileName)
Write data into a file.
|
static void |
writeFileBytes(File file,
byte[] bytes)
Write the entire contents of the given byte array to a file.
|
static void |
writeFileString(File file,
String string,
Charset charset)
Write the entire contents of the given string to a file.
|
static void |
zipRecursively(File from,
File to)
Recursively zips the given input file/directory to the given file
|
static void |
zipRecursively(File from,
ZipOutputStream zipOut)
Recursively zips the given input file/directory to the given zip stream
|
public static boolean deleteDirectory(String dirPath, boolean removeRoot)
dirPath
- the path of the root directoryremoveRoot
- if true the root directory is deleted after the deletion of its childrenpublic static void copy(InputStream in, OutputStream out) throws IOException
in
- The input stream to read from.out
- The output stream to write to.IOException
- if I/O errors occurpublic static boolean deleteDirectory(File directory, boolean removeRoot)
directory
- the root directoryremoveRoot
- if true the root directory is deleted after the deletion of its childrenpublic static <T extends InputStream> T close(T is) throws IOException
T
- the type of streamis
- The streamis=close(is)
IOException
- If an error occurs closing the stream.public static Socket close(Socket socket) throws IOException
socket
- The socketsock=close(sock)
IOException
- If an error occurs closing the socket.public static <T extends OutputStream> T close(T os) throws IOException
T
- the type of streamos
- The streamos=close(os)
IOException
- If an error occurs closing the stream.public static <T extends Closeable> T close(T close) throws IOException
T
- the type of streamclose
- The streamos=close(os)
IOException
- If an error occurs closing the stream.public static void closeQuietly(InputStream is)
is
- the input stream.public static void closeQuietly(Socket socket)
socket
- the socket.public static void closeQuietly(Reader is)
is
- the input stream.public static void closeQuietly(Closeable close)
close
- the input stream to closepublic static void closeQuietly(OutputStream os)
os
- the output stream.public static void deleteRecursivelyWithFailure(File dir) throws IOException
dir
- the directory to deleteIOException
- if unable to deletepublic static void deleteRecursively(File file)
file
- the file/directory to delete.public static void zipRecursively(File from, File to) throws IOException
from
- may be a file or a directoryto
- must not be a directoryIOException
- if an error occurspublic static void zipRecursively(File from, ZipOutputStream zipOut) throws IOException
from
- may be a file or a directory. if a directory, the zip will be rooted at the directory.zipOut
- the zip output streamIOException
- if an error occurspublic static void unzip(File from, File to) throws IOException
from
- may be a fileto
- either directory or fileIOException
- if an error occurspublic static void copyRecursively(File from, File to) throws IOException
from
- the source file/directoryto
- the dest file/directory.IOException
- if I/O errors occurpublic static void copyFile(File from, File to) throws IOException
from
- the source file ( must not be a directory )to
- the dest file ( must not be a directory ).IOException
- if an error occurs while copying the filepublic static Properties loadPropertiesFile(File file) throws IOException
file
- the file to loadIOException
- if I/O errors occur
=public static void savePropertiesFile(File file, Properties properties) throws IOException
file
- the file to save to.properties
- the properties object to save.IOException
- if I/O errors occurpublic static byte[] readFileBytes(File file) throws IOException
file
- the file to read.IOException
- if I/O errors occurpublic static void writeFileBytes(File file, byte[] bytes) throws IOException
file
- the file to write.bytes
- the contents of the file as bytesIOException
- if I/O errors occurpublic static void appendFileBytes(File file, byte[] bytes) throws IOException
file
- the file to write.bytes
- the contents of the file as bytesIOException
- if I/O errors occurpublic static String readFileString(File file, Charset charset) throws IOException
file
- the file to read.charset
- the character encoding of the fileIOException
- if I/O errors occurpublic static byte[] readZipEntry(File zipFile, String entry) throws IOException
zipFile
- the zip fileentry
- the name of theIOException
public static void writeFileString(File file, String string, Charset charset) throws IOException
file
- the file to write.string
- the contents of the file as a stringcharset
- the character encoding for the fileIOException
- if I/O errors occurpublic static void writeData(String data, String fileName)
data
- data to be written into a file.fileName
- the file nameIOException
- if I/O errors occurpublic static String readData(File file)
file
- from which data to be readIOException
- if I/O errors occurpublic static File[] listFilesCanonical(File directory)
directory
- the directory containing the filespublic static int countLines(String outputFile)
Copyright © 2021 Actian Corporation. All rights reserved.