net.simplace.util.service.IOHelper
provides different helper methods for IO
public final class IOHelper {
// Class Methods public static final String
getFileExtension(String aFullFileName);
returns the extension of a given full File name.
public static final String
getFilename(String aFullFileName);
returns the name part of a given full file name (without directory pathes).
public static String
getFilenameWithExtension(String aFullFileName);
returns the name with extension of a given full file name (without directory pathes).
public static final Set
getAllFileNamesInDirectory(String aDirectory, String
aExtension);
returns all the file names with the given extension in a directory.
public static void
writeObjectToFile(File aDestFile, Object aObject)
throws FileNotFoundException, IOException;
Returns the name part of a given full file name (without directory pathes).
public static final Object
readObjectFromFile(File aSourceFile)
throws FileNotFoundException, IOException, Exception;
returns the name part of a given full file name (without directory pathes).
public static final void
copyFiles(File aSourceDir, File aDesDir)
throws IOException;
Copies the content of the given directory to the given targetdirectory.
public static final void
copyFiles(File aSourceDir, File aDesDir, FileFilter
aFileFilter, boolean aShouldOverwrite) throws IOException;
Copies the content of the given directory to the given targetdirectory. Copies only files that
match the given FileFilter.
- aSourceDir
- aDesDir
- aFileFilter
- aShouldOverwrite
public static void
copyDirectory(File aSourceDirectory, File
aDestinationDirectory, boolean aCopySubFolders, boolean aOverwriteCopy)
throws IOException;
copies all contents of one folder to another one.
use the last parameter to define, if you want to copy all subfolders including content
- aSourceDirectory
- aDestinationDirectory
- aCopySubFolders
- aOverwriteCopy
public static final void
copyFile(File aSourceFile, File aDestFile, boolean
aShouldOverwrite) throws FileNotFoundException, IOException;
Copy a file and create the directory structure for
the destination file if it doesnt already exist.
Overwrite the destination file if it already exists.
- aSourceFile
- aDestFile
- aShouldOverwrite
public static final void
moveFile(File aSourceFile, File aDestFile, boolean
aShouldOverwrite) throws FileNotFoundException, IOException;
Copy a file and create the directory structure for
the destination file if it doesnt already exist.
Overwrite the destination file if it already exists.
- aSourceFile
- aDestFile
- aShouldOverwrite
public static final void
addLineToFile(File aFile, String aStringToAppend,
int aMaxKBSizeOfFile) throws IOException;
Constructs a FileWriter object given a File object. String will be written to the end
of the file.
- aFile a File object to write to, if doesn't exist it will be created.
- aStringToAppend String that should be appended to the File
- aMaxKBSizeOfFile max size of File
public static final String
readFirstCharsOfFile(File aFile, int
aNumberOfChars) throws IOException;
Reads the first chars of a File and returns it in a String.
- aFile a File object to read from.
- aNumberOfChars if
true
, then bytes will be written
to the end of the file rather than the beginning
returns the first chars of a File and returns it in a String.
public static final Vector
readLinesOfFile(File aFile) throws IOException;
Reads the last line of a File and returns it in a String.
- aFile a File object to read from.
returns the last line of a File and returns it in a String.
public static final Vector
readLinesOfFile(File aFile, String encoding)
throws IOException;
Reads the last line of a File and returns it in a String.
- aFile a File object to read from.
- encoding if
true
, then bytes will be written
to the end of the file rather than the beginning
returns the last line of a File and returns it in a String.
public static String[][]
readFileToArray(File aFileData, String aDivider)
throws IOException;
Reads a file to an array
- aFileData a File object to read from.
- aDivider if
true
, then bytes will be written
to the end of the file rather than the beginning
returns Double array of file content
public static final Vector
readLinesOfStream(InputStream aInputStream,
String encoding) throws IOException;
Reads the last line of a File and returns it in a String.
- aInputStream a File object to read from.
- encoding if
true
, then bytes will be written
to the end of the file rather than the beginning
returns line vector of stream
public static String[][]
reatStreamToArray(InputStream aInputStream, String
aDivider, String aEncoding) throws IOException;
Reads the last line of a File and returns it in a String.
- aInputStream a File object to read from.
- aDivider divider character
- aEncoding if
true
, then bytes will be written
to the end of the file rather than the beginning
returns a stream to an array with encoding
public static final void
writeLinesOfFile(Vector aLinesVector, File aFile)
throws IOException;
Reads the last line of a File and returns it in a String.
- aFile a File object to read from.
- aLinesVector if
true
, then bytes will be written
to the end of the file rather than the beginning
public static final String
readLastLineOfFile(File aFile) throws IOException;
Reads the last line of a File and returns it in a String.
- aFile a File object to read from.
returns the last line of a File and returns it in a String.
public static final StringBuffer
stringBufferFromInputStream(InputStream
aInputStream) throws IOException;
Read the contents of a String from an InputStream.
returns the contents of a String from an InputStream.
public static final StringBuffer
stringBufferFromInputStream(InputStream
aInputStream, String aEncoding) throws IOException;
Read the contents of a String from an InputStream.
returns the contents of a String from an InputStream.
public static final String
stringFromInputStream(InputStream aInputStream)
throws IOException;
Read the contents of a String from an InputStream.
returns the contents of a String from an InputStream.
public static final String
stringFromInputStream(InputStream aInputStream,
String aEncoding) throws IOException;
Read the contents of a String from an InputStream.
returns the contents of a String from an InputStream.
public static final String
encodedStringFromInputStream(InputStream
aInputStream, String aEncoding) throws IOException;
Returns the contents of a String from an InputStream in the given encoded format.
returns the contents of a String from an InputStream.
public static final String
encodedStringFromInputStream(InputStream
aInputStream) throws IOException;
Returns the contents of a String from an InputStream in the UTF-8 encoded format.
returns the contents of a String from an InputStream.
public static final String
platformDefaultToUTF8(String aString)
throws IOException;
Turns a String which was created using the platform standard encoding
to an String which is encoded using UTF-8 encoding.
returns a String as UTF encoding
public static final boolean
isXMLFileExtension(String aExtension);
returns if the extension belongs to an UTF-8 encoded XML file.
public static final String
UTF8ToPlatformDefault(String aString)
throws IOException;
Turns a String which was created using the UTF-8 encoding
to an String which is encoded using platform standard encoding.
returns a String which was created using the UTF-8 encoding
public static final void
stringToOutputStream(String aString, OutputStream
aOutputStream) throws IOException;
Write the contents of a String to an OutputStream.
The Stream won't be closed at the end.
public static final void
stringBufferToOutputStream(StringBuffer
aStringBuffer, OutputStream aOutputStream) throws IOException;
Write the contents of a StringBuffer to an OutputStream.
The Stream won't be closed at the end.
- aStringBuffer
- aOutputStream
public static final void
stringToOutputStreamAndClose(String aString,
OutputStream aOutputStream) throws IOException;
Write the contents of a String to an OutputStream.
The Stream will be closed at the end.
public static final void
stringToOutputStream(String aString, OutputStream
aOutputStream, String aEncoding) throws IOException;
Write the contents of a String to an OutputStream in the given encoding.
The Stream won't be closed at the end.
- aString
- aOutputStream
- aEncoding
public static final void
stringToOutputStreamAndClose(String aString,
OutputStream aOutputStream, String aEncoding) throws IOException;
Write the contents of a String to an OutputStream in the given encoding.
The Stream will be closed at the end.
- aString
- aOutputStream
- aEncoding
public static final void
stringToFile(String aString, File aFile, String
aEncoding) throws IOException;
Write the contents of a String to a File in the given encoding.
The Stream will be closed at the end.
public static final void
stringToFile(String aString, File aFile)
throws IOException;
Write the contents of a String to a File in platform standard encoding.
The Stream will be closed at the end.
public static final void
fileToOutputStream(File aFile, OutputStream
aOutputStream) throws IOException;
Copy data from a file to an OututStream.
public static final void
fileFromInputStream(File aFile, InputStream
aInputStream) throws IOException;
Copy data from an InputStream to a file.
public static final void
copyStream(InputStream aInputStream, OutputStream
aOutputStream) throws IOException;
Copy data from an InputStream to an OutputStream.
- aInputStream
- aOutputStream
public static final void
copyStream(Reader aReader, Writer aWriter)
throws IOException;
Copy data from an Reader to an Writer.
public static final void
createDir(File aDir) throws IOException;
Create a directory including its parent
directories if they dont already exist.
public static final boolean
deleteFile(File aFile);
Delete a file if it exists.
returns success
public static final List
getFiles(File aFile, boolean aShouldIncludeSubdirs);
- aFile
- aShouldIncludeSubdirs
returns all the file objects within a directory(and also sub directories) excluding
the directories in it (Excluding the directory file objects.).
public static final List
getFilesForPattern(File aFile, String
aRegexpPattern, boolean aShouldIncludeSubdirs, boolean aIgnoreCase);
- aFile
- aRegexpPattern
- aShouldIncludeSubdirs
- aIgnoreCase
returns all the file objects within a directory(and also sub directories) excluding
the directories in it (Excluding the directory file objects.).
public static final void
deleteFiles(File aFile) throws IOException;
Deletes all the files in a directory including the sub directories files, if the given file
is a directory.
!!!The sub directories won't be deleted!!!
public static void
deleteFiles(File aSourceDir, FileFilter aFileFilter);
- aSourceDir
- aFileFilter
Deletes all the files accepted in the fileFilter returning true if successful
public static final boolean
deleteFiles(Collection aFilesCollection);
Deletes all the files in the given FilesCollection returning true if successful
returns success
public static final boolean
deleteDir(File dir);
Deletes all the files in a directory including the sub directories files, if the given file
is a directory and also the directory.
returns success
public static final boolean
deleteDir(File dir, boolean goOnWithError);
Deletes all the files in a directory including the sub directories files, if the given file
is a directory and also the directory.
returns success
public static final boolean
isEmpty(File aDirectory);
returns if the given directory is an empty directory including sub directories.
public static final boolean
deleteDirContents(File dir, boolean goOnOnError);
Deletes all the files in a directory including the sub directories files, if the given file
is a directory but does not delete the directory itself.
returns success
public static final boolean
deleteDirContents(File aDir, String aExtension,
long aOlderThanMillis);
Deletes all the files in a directory including the sub directories files, if the given file
is a directory but does not delete the directory itself. It only deletes files with given
extension (or null for all) if last modified date is older than given long-date.
- aDir
- aExtension
- aOlderThanMillis
returns success
public static final Collection
getDirContents(File aDir, String aExtension,
long aOlderThanMillis);
- aDir
- aExtension
- aOlderThanMillis
returns Collection of Files fitting given criteria
public static final Collection
getDirectoriesOnly(File aDir);
returns Collection of Directories in aDir
public static final boolean
deleteDirContents(File aDir, String aExtension);
Deletes all the files in a directory including the sub directories files, if the given file
is a directory but does not delete the directory itself.It only deletes files with given
extension
returns success
public static final String
returnValidFilename(String aUncheckedFilename);
Replaces all letters that are maybe not supported by system for using in filename.
You should only give the filename without path!!!
returns valid file name
public static final String
returnValidPath(String aUncheckedFilename);
Replaces all letters that are maybe not supported by system for using in filename.
You should only give the filename without path!!!
If add/remove characters also change isValidFilename/getInvalidFilenameCharacters
returns valid file name
public static final boolean
isValidFilename(String aFilename);
You should only give the filename without path!!!
If add/remove characters also change returnValidPath/getInvalidFilenameCharacters
returns if the given filename is valid
public static final String getInvalidFilenameCharacters();
If add/remove characters also change returnValidPath/isValidFilename
returns a string containing all invalid characters.
public static String
getFileSizeString(long aByte);
returns a string describing the given byte size
public static String getHostNameOfLocalSystem() throws UnknownHostException;
returns HostName of localhost.
public static String getIPOfLocalSystem() throws UnknownHostException;
returns IP of localhost.
public static String
getTemporaryExecutionFilePath(String aFilePath)
throws IOException;
returns If the File with the path is used by an other program a new one wit ~x is created and returned
public static boolean
doesPatternFit(String aFileName, List aPatternList,
boolean aIgnoreCase);
- aFileName
- aPatternList
- aIgnoreCase
returns if the given text fits to any of the given patterns.
public static boolean
doesPatternFit(String aFileName, String aPattern,
boolean aIgnoreCase);
- aFileName
- aPattern
- aIgnoreCase
returns if the given patterns fit .
public static String
getRelativePath(File aFromDir, File aToFileOrDir)
throws IOException;
returns the relative path. The first file is the directory, the second is the file.
public static Collection
fillFileCollection(File aDirectory, Collection
aFileCollection, boolean aIncludeDirectories);
- aDirectory
- aFileCollection
- aIncludeDirectories
returns all files from the collection including dirs if wanted.
public static Collection
fillFileCollection(File aDirectory, Collection
aFileCollection, String aExtensionFilter);
- aDirectory
- aFileCollection
- aExtensionFilter
returns all files from the collection without directories but only with given Extension - without extension all files are returned.
public static long
getFileSize(File aFile, boolean aRecursive);
Returns the size of the given file. If its a directory it will return the total size of all
files in it (including the sub directories).
returns file size
public static ArrayList
getFilesWithExtension(File aDirectory, String
aExtension);
- aDirectory Directory in which too search
- aExtension Extension to look for
returns all files in a directory with a given extension
public static ArrayList
getFilesWithExtension(File aDirectory, String
aExtension, boolean aIncludeSubDirs);
- aDirectory Directory in which too search
- aExtension Extension to look for
- aIncludeSubDirs
returns all files in a directory with a given extension
public static byte[]
getBytesFromFile(File aFile) throws IOException;
returns the contents of the file in a byte array.
public static boolean
isInDirectory(File aFile, File aDirectory);
returns if the given file is a subfile/-directory of the given directory.
public static boolean
createLockFile(File aDirectory);
Locks the system.
returns success
public static File
getFile(Collection aFileCollection, String aFileName);
- aFileCollection
- aFileName
returns the given fileList for the also given fileName.
public static URL
checkURL(String aURL);
returns the checked URL or null or MalformedURLException
public static boolean
areRessourcesCached(Set aRessouceList);
returns if all parts of Resources are cached for Webstart applications
public static void
downloadRessources(Set aRessourceList);
checks if resources are available. If not, they are download
public static File
chooseFile(String aExtension);
returns the chosen file
public static boolean
doesFileExist(String aPath, boolean
aSouldAcceptDirectories);
- aPath
- aSouldAcceptDirectories
returns if File exists
public static String[][]
readCSVtoArray(File aFile, String aDivider)
throws IOException;
returns the content of the CSV as Array
public static Object
resizeArray(Object oldArray, int newSize);
Reallocates an array with a new size, and copies the contents of the old array to the new array.
- oldArray
the old array, to be reallocated.
- newSize
the new array size.
returns A new array with the same contents.
public static void
writeArrayToFile(Object[][] aSumOfCutValues, File aFile,
String aString) throws IOException;
- aSumOfCutValues
- aFile
- aString
public static void
fillWithZero(Number[][] aValuesArray);
}