public class FileTool extends Object
| Modifier and Type | Method and Description |
|---|---|
static void |
closeAll(Object... list)
This attempts to close all of the resources passed to it, without throwing exceptions.
|
static void |
compareDirectories(IDirectoryDelta delta,
File a,
File b)
Compare the content of two directories, and callback methods on changes.
|
static InputStream |
copyAndDumpStream(StringBuilder tgt,
InputStream in,
String encoding) |
static void |
copyDir(File destd,
File srcd)
Copies an entire directory structure from src to dest.
|
static void |
copyFile(File destf,
File srcf)
Copies a file.
|
static void |
copyFile(OutputStream os,
InputStream is)
Copies the inputstream to the output stream.
|
static void |
copyFile(Writer w,
Reader r)
Copy the input reader to the output reader.
|
static void |
copyHardlinked(File targetDir,
File sourceDir,
String... ignorePaths)
Copy a file or directory using hard links.
|
static void |
copyResource(Writer w,
Class<?> cl,
String rid) |
static File |
copyStreamToTmpFile(InputStream is) |
static File |
copyStreamToTmpFile(InputStream is,
String name) |
static File |
createTmpDir() |
static void |
deleteDir(File f)
Delete the directory and all it's contents.
|
static boolean |
dirEmpty(File dirf)
Deletes all files in the directory.
|
static boolean |
dirEmpty(File dirf,
Vector<Object> elogb)
Deprecated.
|
static String |
fileNameSansExtension(String fn)
Returns the file name excluding the suffix of the name.
|
static int |
findFilenameExtension(String fn)
Returns the start position of the filename extension in the string.
|
static String |
getFileExtension(String fn)
Returns the extension of a file.
|
static int |
getIntSizeOfFile(File file)
Returns size of a file as int type.
|
static File |
getLogRoot(String appVar)
This returns the File location of a directory that should contain application-generated
log files.
|
static int |
getNumberOfLines(File file)
Returns number of lines in a specified file.
|
static String |
getRelativePath(File root,
File other)
Calculate the relative path of file in the root passed.
|
static Reader |
getResourceReader(Class<?> root,
String name) |
static File |
getTmpDir()
Returns the java.io.tmpdir directory.
|
static InputStream |
getZipContent(File src,
String name) |
static InputStream |
getZipContent(InputStream zipis,
String name)
Returns a stream which is the uncompressed data stream for a zip file
component.
|
static List<String> |
getZipDirectory(File in) |
static byte[] |
hashBuffers(byte[][] data)
Create an MD5 hash for a buffer set.
|
static String |
hashBuffersHex(byte[][] data)
Create a HEX MD5 hash for a buffer set.
|
static byte[] |
hashFile(File f)
Create an MD5 hash for a file's contents.
|
static byte[] |
hashFile(InputStream is)
Hashes all data from an input stream and returns an MD5 hash.
|
static String |
hashFileHex(File f)
Hash a file and return it's hex MD5hash.
|
static String |
hashFileHex(InputStream is)
Hash an InputStream and return it's hex MD5hash.
|
static String |
hashTextFile(File f)
Hashes all data from an input stream and returns an MD5 hash in hex.
|
static byte[] |
hashTextFile(InputStream is)
Hashes all data from an input stream and returns an MD5 hash.
|
static byte[] |
loadArray(File src) |
static byte[][] |
loadByteBuffers(File in)
Load an entire file in a byte buffer set.
|
static byte[][] |
loadByteBuffers(InputStream is)
Loads a byte[][] from an input stream until exhaustion.
|
static Properties |
loadProperties(File f)
Load a file as a Properties file.
|
static Properties |
loadPropertiesFromZip(File f,
String name)
Opens the jar file and tries to load the plugin.properties file from it.
|
static Properties |
loadPropertiesFromZip(InputStream is,
String name)
Opens the jar file and tries to load the plugin.properties file from it.
|
static Object |
loadSerialized(File f)
Load a single serialized object from a file.
|
static Object |
loadSerialized(InputStream is)
Load a single serialized object from a datastream.
|
static Object |
loadSerializedNullOnError(File f) |
static Object |
loadSerializedNullOnError(InputStream is)
Load a serialized object, and return null on any load exception.
|
static Document |
loadXmlFromZip(File f,
String name,
boolean nsaware)
Opens the jar file and tries to load the plugin.properties file from it.
|
static Document |
loadXmlFromZip(InputStream is,
String ident,
String name,
boolean nsaware)
Opens the jar file and tries to load the plugin.properties file from it.
|
static ClassLoader |
makeJarLoader(File f)
Creates a classloader to load data from the given jar file.
|
static ClassLoader |
makeJarLoader(File f,
ClassLoader parent) |
static File |
makeTempFile(File root)
Create a temp file within the specified root directory.
|
static File |
newDir(File root)
Create a temp directory within the root directory.
|
static void |
prepareDir(File dir)
prepare a directory in this way: if it does not exist, create it.
|
static String |
readAsString(Clob data)
Returns the string from specified Clob.
|
static byte[] |
readByteArray(InputStream is) |
static byte[] |
readFileAsByteArray(File file)
Read a file's contents as byte[].
|
static void |
readFileAsString(Appendable o,
File f) |
static String |
readFileAsString(File f)
Read a file's contents in a string using the default encoding of the platform.
|
static String |
readFileAsString(File f,
String encoding)
Read a file into a string using the specified encoding.
|
static void |
readHeadAndTail(StringBuffer sb,
File f,
int headsize,
int tailsize)
mbp, moved here from old DaemonBase with some adaptions.
|
static int |
readInt(InputStream is)
Reads a 4-byte bigendian int off the connection.
|
static long |
readLong(InputStream is) |
static byte[] |
readResourceAsByteArray(Class<?> clz,
String name)
Load a class resource as a byte array.
|
static String |
readResourceAsString(Class<?> base,
String name,
String encoding) |
static void |
readStreamAsString(Appendable o,
InputStream f,
String enc) |
static void |
readStreamAsString(Appendable o,
Reader r) |
static String |
readStreamAsString(InputStream is,
String enc) |
static String |
readStreamAsString(Reader r) |
static String |
readString(InputStream is) |
static void |
save(File of,
byte[] data)
Save the data in byte array to a file.
|
static void |
save(File of,
byte[][] data)
Save the data in byte buffers to a file.
|
static void |
save(OutputStream os,
byte[][] data)
Save the data in byte buffers to an output stream.
|
static void |
saveBlob(File out,
Blob in)
Saves blob into specified file.
|
static void |
saveProperties(File f,
Properties p)
Save a properties file.
|
static void |
saveSerialized(File f,
Serializable obj) |
static void |
saveSerialized(OutputStream os,
Serializable obj)
Save a serializable object to a datastream.
|
static void |
unzip(File dest,
File zipfile)
Unzip the contents of the zipfile to the directory.
|
static void |
unzip(File dest,
InputStream is) |
static InputStream |
wrapInputStream(InputStream rawStream,
ILogSink s,
int maxinmemory) |
static void |
writeFileFromString(File f,
String v,
String enc) |
static void |
writeFileFromString(OutputStream os,
String v,
String enc) |
static void |
writeInt(OutputStream os,
int val)
Sends an int fragment
|
static void |
writeLong(OutputStream os,
long val)
Sends a long
|
static void |
writeString(OutputStream os,
String s) |
static void |
zip(File zipfile,
File dir)
Zip the contents of dir or file to the zipfile.
|
static void |
zipAppend(ZipOutputStream zos,
String fileName,
File f) |
@Nonnull public static File getLogRoot(@Nonnull String appVar)
public static File getTmpDir()
public static File newDir(File root)
root - public static File makeTempFile(File root)
root - public static boolean dirEmpty(File dirf)
public static void deleteDir(File f)
f - public static void prepareDir(File dir) throws Exception
dir - the directory that must be made existentException - when creation fails or when removing old contents
fails.@Deprecated public static boolean dirEmpty(File dirf, Vector<Object> elogb)
@Nonnull public static String getFileExtension(String fn)
public static int findFilenameExtension(String fn)
@Nonnull public static String fileNameSansExtension(String fn)
public static void copyFile(File destf, File srcf) throws IOException
destf - the destinationsrcf - the sourceIOException - the errorpublic static void copyFile(OutputStream os, InputStream is) throws IOException
destf - the destinationsrcf - the sourceIOException - the errorpublic static void copyFile(Writer w, Reader r) throws IOException
w - r - IOExceptionpublic static void copyDir(File destd, File srcd) throws IOException
destd - srcd - IOExceptionpublic static void copyHardlinked(@Nonnull File targetDir, @Nonnull File sourceDir, String... ignorePaths) throws IOException
targetDir - sourceDir - IOExceptionpublic static String readFileAsString(File f) throws Exception
f - Exceptionpublic static byte[] readFileAsByteArray(@Nonnull File file) throws IOException
f - IOExceptionException@Nonnull public static byte[] readByteArray(@Nonnull InputStream is) throws Exception
Exception@Nullable public static byte[] readResourceAsByteArray(@Nonnull Class<?> clz, @Nonnull String name) throws IOException
clz - name - IOExceptionpublic static void readFileAsString(Appendable o, File f) throws Exception
o - f - Exceptionpublic static String readFileAsString(File f, String encoding) throws Exception
f - encoding - Exceptionpublic static void readHeadAndTail(StringBuffer sb, File f, int headsize, int tailsize) throws Exception
Exceptionpublic static File copyStreamToTmpFile(InputStream is, String name) throws Exception
is - Exceptionpublic static File copyStreamToTmpFile(InputStream is) throws Exception
is - Exceptionpublic static String readStreamAsString(InputStream is, String enc) throws Exception
Exceptionpublic static void readStreamAsString(Appendable o, InputStream f, String enc) throws Exception
Exceptionpublic static void readStreamAsString(Appendable o, Reader r) throws Exception
Exceptionpublic static String readStreamAsString(Reader r) throws Exception
Exceptionpublic static void writeFileFromString(File f, String v, String enc) throws Exception
Exceptionpublic static void writeFileFromString(OutputStream os, String v, String enc) throws Exception
Exception@Nonnull public static final String readResourceAsString(Class<?> base, String name, String encoding) throws Exception
Exception@Nonnull public static byte[] hashFile(File f) throws IOException
IOException@Nonnull public static byte[] hashBuffers(byte[][] data)
data - @Nonnull public static String hashBuffersHex(byte[][] data)
data - @Nonnull public static byte[] hashFile(InputStream is) throws IOException
is - The stream to read and hash.IOException@Nonnull public static String hashFileHex(File f) throws IOException
f - IOException@Nonnull public static String hashFileHex(InputStream is) throws IOException
is - IOException@Nonnull public static String hashTextFile(@Nonnull File f) throws IOException
f - IOException@Nonnull public static byte[] hashTextFile(@Nonnull InputStream is) throws IOException
is - The stream to read and hash.IOException@Nonnull public static Properties loadProperties(File f) throws Exception
f - Exceptionpublic static void saveProperties(File f, Properties p) throws Exception
f - p - Exceptionpublic static Properties loadPropertiesFromZip(File f, String name) throws Exception
f - Exceptionpublic static Properties loadPropertiesFromZip(InputStream is, String name) throws Exception
f - Exceptionpublic static Document loadXmlFromZip(File f, String name, boolean nsaware) throws Exception
f - Exceptionpublic static Document loadXmlFromZip(InputStream is, String ident, String name, boolean nsaware) throws Exception
f - Exceptionpublic static ClassLoader makeJarLoader(File f) throws MalformedURLException
f - MalformedURLExceptionExceptionpublic static ClassLoader makeJarLoader(File f, ClassLoader parent) throws MalformedURLException
MalformedURLExceptionpublic static void copyResource(Writer w, Class<?> cl, String rid) throws Exception
Exceptionpublic static void zip(File zipfile, File dir) throws Exception
Exceptionpublic static void zipAppend(ZipOutputStream zos, String fileName, File f) throws IOException
IOExceptionpublic static void unzip(File dest, InputStream is) throws Exception
Exceptionpublic static void unzip(File dest, File zipfile) throws Exception
Exception@Nonnull public static List<String> getZipDirectory(@Nonnull File in) throws Exception
Exception@Nullable public static InputStream getZipContent(File src, String name) throws IOException
IOException@Nullable public static InputStream getZipContent(@Nonnull InputStream zipis, @Nonnull String name) throws IOException
zipis - IOException@Nonnull public static byte[][] loadByteBuffers(@Nonnull InputStream is) throws IOException
is - IOExceptionpublic static byte[][] loadByteBuffers(File in) throws IOException
in - IOExceptionpublic static void save(File of, byte[][] data) throws IOException
of - data - IOExceptionpublic static void save(OutputStream os, byte[][] data) throws IOException
os - data - IOException@Nonnull public static byte[] loadArray(@Nonnull File src) throws IOException
IOExceptionpublic static void save(File of, byte[] data) throws IOException
of - data - IOExceptionpublic static void writeInt(OutputStream os, int val) throws IOException
val - ExceptionIOExceptionpublic static void writeLong(OutputStream os, long val) throws IOException
val - ExceptionIOExceptionpublic static void writeString(OutputStream os, String s) throws IOException
IOExceptionpublic static int readInt(InputStream is) throws IOException
ExceptionIOExceptionpublic static long readLong(InputStream is) throws IOException
IOExceptionpublic static String readString(InputStream is) throws IOException
IOExceptionpublic static InputStream wrapInputStream(InputStream rawStream, ILogSink s, int maxinmemory) throws Exception
Exceptionpublic static InputStream copyAndDumpStream(StringBuilder tgt, InputStream in, String encoding) throws IOException
IOExceptionpublic static void saveSerialized(@WillClose OutputStream os, Serializable obj) throws IOException
os - obj - IOExceptionpublic static void saveSerialized(File f, Serializable obj) throws IOException
IOException@Nullable public static Object loadSerialized(@WillNotClose InputStream is) throws IOException, ClassNotFoundException
is - IOExceptionClassNotFoundException@Nullable public static Object loadSerialized(File f) throws IOException, ClassNotFoundException
f - IOExceptionClassNotFoundException@Nullable public static Object loadSerializedNullOnError(@WillNotClose InputStream is)
is - @Nullable public static Object loadSerializedNullOnError(File f) throws IOException, ClassNotFoundException
IOExceptionClassNotFoundExceptionpublic static void closeAll(@WillClose Object... list)
list - public static void compareDirectories(IDirectoryDelta delta, File a, File b) throws Exception
Exceptionpublic static void saveBlob(@Nonnull File out, @Nonnull Blob in) throws Exception
out - in - Exceptionpublic static int getIntSizeOfFile(@Nonnull File file)
Integer.MAX_VALUE, throws IllegalStateException.
This can be used on files with expected size less then 2GB.file - @Nonnull public static File createTmpDir() throws IOException
IOExceptionpublic static int getNumberOfLines(@Nonnull File file) throws IOException
file - IOException@Nullable public static String getRelativePath(@Nonnull File root, @Nonnull File other)
root - other - Exception@Nonnull public static Reader getResourceReader(@Nonnull Class<?> root, @Nullable String name)
@Nonnull public static String readAsString(@Nonnull Clob data) throws IOException, SQLException
data - SQLExceptionIOExceptionCopyright © 2017 etc.to. All rights reserved.