public class IOUtil extends Object
Modifier and Type | Method and Description |
---|---|
static long |
checksum(File file) |
static long |
checksum(String fileName)
Calculates the CRC32 checksum of the specified file.
|
static boolean |
copyFile(File src,
File dest)
Copies a file to a destination.
|
static long |
copyFile(InputStream fis,
OutputStream fos)
Copies one file to another.
|
static void |
delete(File f)
Recursively delete all the files in a directory and the directory.
|
static void |
extractResourceToFile(Class<?> clazz,
String path,
File file)
Extracts the resource to a file.
|
static byte[] |
getResourceAsBytes(Class<?> clazz,
String res)
Get the resource as a byte array.
|
static Properties |
getResourceAsProperties(ClassLoader loader,
String path)
Loads the given resource as a properties object.
|
static InputStream |
getResourceAsStream(Class<?> clazz,
String res)
Returns an input stream of the resource specified.
|
static String |
getResourceAsString(Class<?> clazz,
String res)
Read the entire stream into a String and return it.
|
static String |
getResourceAsString(Class<?> clazz,
String res,
Charset charset)
Read the entire stream into a String and return it.
|
static String |
getResourcePath(Class<?> c,
String res)
Get the path to a resource base on the package of given class.
|
static byte[] |
inputStreamToBytes(InputStream ins)
Takes a 'InputStream' and returns a byte array.
|
static Properties |
loadPropertiesFile(File f)
Loads the given file as a Properties file.
|
static Properties |
loadPropertiesFile(String string)
Attempt to load file based on a string base filename.
|
static URL |
makeURL(File dir,
String path)
Make a URL from a directory and path.
|
static void |
quietClose(InputStream stream)
Quietly closes the stream.
|
static void |
quietClose(OutputStream stream)
Quietly closes the stream.
|
static void |
quietClose(Reader reader)
Quietly closes the reader.
|
static void |
quietClose(Writer writer)
Quietly closes the Writer.
|
static String |
readerToString(Reader rdr)
Takes a 'Reader' and returns the contents as a string.
|
static byte[] |
readFileBytes(File file)
Reads the given file as bytes
|
static String |
readFileUTF8(File file)
Reads the given file as UTF-8
|
static byte[] |
readInputStreamBytes(InputStream is,
boolean close)
Reads an entire file and returns the bytes.
|
static void |
storePropertiesFile(File f,
Properties properties)
Stores the given file as a Properties file.
|
static void |
unjar(JarFile jarFile,
File toDir)
Unjars the given file to the given directory.
|
static void |
writeFileUTF8(File file,
String contents)
Write the contents of the string out to a file in UTF-8 format.
|
public static final String UTF8
public static void quietClose(Reader reader)
IOException
which may be thrown and ignore it.reader
- - Reader to closepublic static void quietClose(InputStream stream)
IOException
which may be thrown.stream
- - Stream to closepublic static void quietClose(Writer writer)
writer
- - Writer to closepublic static void quietClose(OutputStream stream)
stream
- - Stream to closepublic static String getResourcePath(Class<?> c, String res)
c
- Class to get the package path too.res
- Name of the resource to get the path of.public static InputStream getResourceAsStream(Class<?> clazz, String res)
public static byte[] getResourceAsBytes(Class<?> clazz, String res)
public static String getResourceAsString(Class<?> clazz, String res, Charset charset)
public static String getResourceAsString(Class<?> clazz, String res)
public static byte[] inputStreamToBytes(InputStream ins)
public static String readerToString(Reader rdr)
rdr
- Producer for the string data.public static boolean copyFile(File src, File dest) throws IOException
src
- The source must be a filedest
- This can be a directory or a file.IOException
public static long copyFile(InputStream fis, OutputStream fos) throws IOException
NOTE: does not close streams.
IOException
public static long checksum(String fileName) throws IOException, FileNotFoundException
fileName
- - the path to the file on which to calculate the checksumIOException
FileNotFoundException
public static long checksum(File file) throws IOException, FileNotFoundException
IOException
FileNotFoundException
public static byte[] readInputStreamBytes(InputStream is, boolean close) throws IOException
close
- if true, close when finished reading.IOException
public static void delete(File f) throws IOException
RuntimeException
- iff there is file that can not be deleted.IOException
public static Properties loadPropertiesFile(File f) throws IOException
IOException
public static void storePropertiesFile(File f, Properties properties) throws IOException
IOException
public static Properties getResourceAsProperties(ClassLoader loader, String path) throws IOException
loader
- The class loaderpath
- The path to the resourceIOException
- If an error occurs reading itpublic static void extractResourceToFile(Class<?> clazz, String path, File file) throws IOException
clazz
- The class, relative to which path is resolvedpath
- The path to the resourcefile
- The file to extract toIOException
- If an error occurs reading itpublic static void unjar(JarFile jarFile, File toDir) throws IOException
jarFile
- The file to unjar.toDir
- The directory to unjar to.IOException
public static String readFileUTF8(File file) throws IOException
file
- The file to readIOException
- iff there is an issue reading the file.public static byte[] readFileBytes(File file) throws IOException
file
- The file to readIOException
- iff there is an issue reading the file.public static void writeFileUTF8(File file, String contents) throws IOException
file
- the file to write to.contents
- the contents of the file to write to.IOException
- iff there is an issue writing the file.NullPointerException
- iff the file parameter is null.public static URL makeURL(File dir, String path) throws IOException
dir
- directory to start from.path
- file or path to create the url.IOException
- iff the URL create from the parameters does not specify a file.public static Properties loadPropertiesFile(String string) throws IOException
string
- represents the file.IOException
- if there is an issue.Copyright © 2013. All Rights Reserved.