com.kuka.common
Class FileUtil

java.lang.Object
  extended by com.kuka.common.FileUtil

public final class FileUtil
extends Object

Provides utility methods for working on the file system.

This element is an undocumented internal feature. It is not intended to be used by applications as it might change or be removed in future versions.

Field Summary
static FilenameFilter ConfigFilesFilter
          File filter for Configuration files (= *.xml and *.kef).
static FilenameFilter XmlFilesFilter
          File filter for XML files.
 
Method Summary
static boolean areBinaryEqual(File file1, File file2)
          Checks whether the two files are binary equal. offset.
static boolean areBinaryEqual(File file1, File file2, int offset)
          Checks whether the two files are binary equal starting with a given offset.
static String combinePaths(String... paths)
          Puts the given path fragments together.
static void copyFile(File source, File destination)
          copy a file from the source to the destination location.
static void copyFile(InputStream in, File destination)
          Copies an input stream to a destination file.
static void copyFile(String source, String destination)
          copy a file from the source to the destination location.
static void copyFileRecursive(File source, File destination, IProgressListener listener)
          copies a directory with all sub-directories an containing files from the source to the destination location.
static void copyFiles(File source, File destination)
          Copies a source file to a destination.
static void copyFiles(File source, File destination, boolean stopOnError)
          Copies a directory with all sub-directories and containing files from the source to the destination location.
static void createAndFillFile(File file, String content)
          Creates and fills a file with the specified content, using the default charset.
static void createAndFillFile(File file, String content, Charset charset)
          Creates and fills a file with the specified content using the given charset.
static void createAndFillFileAndDir(File file, String content)
          Creates and fills a file with the specified content.
static void createAndFillFileAndDir(File file, URL url)
          Creates and fills a file with the specified content.
static File createTempDir()
          Creates a new directory in the system's default temporary-file directory.
static File createTemporaryFolder(String prefix, String suffix, File directory)
          Creates a new temporary folder.
static File createUniqueFile(String prefix, String suffix, File directory)
          Creates a new file with the given prefix and suffix in the given directory.
static boolean deleteDirectory(File dir)
          Deletes all files recursively in the given directory.
static boolean deleteDirectory(File dir, boolean stopOnError)
          Deletes all files recursively in the given directory.
static void deleteDirectoryContent(File directory)
          deletes the content of the specified directory.
static void deleteEmptyDirectories(File directory)
          Deletes the directory structure of directories without files.
static List<File> findFile(String fileName, File rootDirectory)
          searches for a file within the root directory and all sub-directories.
static String getAbsolutePath(String path)
          Gets the absolute path of the given path without filename.
static List<String> getDirectoryNames(File rootDirectory)
          Gets a list of all subdirectories of the given root directory.
static byte[] getFileAsByteArray(File file)
          Gets a file as byte array.
static byte[] getFileAsByteArray(String filename)
          Gets a file as byte array.
static String getFileAsString(URL url)
          Gets the complete content of a file as string.
static String getFileExtension(File file)
          Gets the extension of the file.
static String getFileName(String path)
          Gets the filename of the given path.
static String getFilenameWithoutExtension(File file)
          Gets the name of the file without the extension.
static String getPath(String path)
          Gets the path of the given path without a filename.
static byte[] getStreamAsByteArray(InputStream in)
          Copies the content of the stream into a byte array.
static String getVendorFromManifest(File jarFile)
          Gets the vendor string from a jar file.
static List<String> readStringsFromFile(File inputFile)
          Reads the lines from a UTF-8 encoded text file into a list of strings, each line into one string.
static void writeFileContent(File file, byte[] fileContent)
          Writes bytes into a file.
static void writeStringsToFile(File file, List<String> lines)
          Writes the given strings one per line into a UTF-8 encoded text file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

XmlFilesFilter

public static final FilenameFilter XmlFilesFilter
File filter for XML files.


ConfigFilesFilter

public static final FilenameFilter ConfigFilesFilter
File filter for Configuration files (= *.xml and *.kef).

Method Detail

deleteDirectory

public static boolean deleteDirectory(File dir)
Deletes all files recursively in the given directory.

Parameters:
dir - The directory.
Returns:
True if everything was ok.

deleteDirectory

public static boolean deleteDirectory(File dir,
                                      boolean stopOnError)
Deletes all files recursively in the given directory.

Parameters:
dir - The directory.
stopOnError - stops the deletion process when a file cannot be deleted. Otherwise the file will be marked as to delete and the process will be continued...
Returns:
True if everything was ok.

deleteDirectoryContent

public static void deleteDirectoryContent(File directory)
                                   throws IOException
deletes the content of the specified directory.

Parameters:
directory - the directory whose content should be deleted
Throws:
IOException - if the contents could not be deleted

deleteEmptyDirectories

public static void deleteEmptyDirectories(File directory)
Deletes the directory structure of directories without files. This method can be used to removed unused directory structures without the risk of accidently deleting files within the directories.

Parameters:
directory - the directory to delete

copyFile

public static void copyFile(String source,
                            String destination)
                     throws IOException
copy a file from the source to the destination location.

Parameters:
source - source file
destination - destination file
Throws:
IOException - source file was not found, destination file cannot be created

copyFile

public static void copyFile(File source,
                            File destination)
                     throws IOException
copy a file from the source to the destination location.

Parameters:
source - source file
destination - destination file
Throws:
IOException - source file was not found, destination file cannot be created

copyFile

public static void copyFile(InputStream in,
                            File destination)
                     throws IOException
Copies an input stream to a destination file.

Parameters:
in - The input stream. Will be closed on return or exception.
destination - The destination file
Throws:
IOException - if the stream or the file could not be accessed

copyFiles

public static void copyFiles(File source,
                             File destination,
                             boolean stopOnError)
                      throws IOException
Copies a directory with all sub-directories and containing files from the source to the destination location.

Parameters:
source - the source location
destination - the destination location
stopOnError - if true, an exception is thrown if an error occurs, otherwise the recursive file copy continues
Throws:
IOException - source file was not found, destination file cannot be created

copyFiles

public static void copyFiles(File source,
                             File destination)
                      throws IOException
Copies a source file to a destination.

Parameters:
source - The source file
destination - The destination file
Throws:
IOException - The Exception is thrown if one of the files could not be accessed

getFileName

public static String getFileName(String path)
Gets the filename of the given path.

Parameters:
path - A path.
Returns:
The filename of the given path.

getPath

public static String getPath(String path)
Gets the path of the given path without a filename.

Parameters:
path - A path.
Returns:
The path without filename.

getAbsolutePath

public static String getAbsolutePath(String path)
Gets the absolute path of the given path without filename.

Parameters:
path - A path.
Returns:
The absolute path without filename.

copyFileRecursive

public static void copyFileRecursive(File source,
                                     File destination,
                                     IProgressListener listener)
                              throws IOException
copies a directory with all sub-directories an containing files from the source to the destination location.

Parameters:
source - the source location
destination - the destination location
listener - installation listener that logs the copy process (optional)
Throws:
IOException - source file was not found, destination file cannot be created

findFile

public static List<File> findFile(String fileName,
                                  File rootDirectory)
searches for a file within the root directory and all sub-directories.

Parameters:
fileName - name of the file to search
rootDirectory - directory where the file should be searched
Returns:
all found occurrences of the file

createTempDir

public static File createTempDir()
Creates a new directory in the system's default temporary-file directory. The caller of this method should ensure to delete the directory after use.

Returns:
The temporary directory created.

createTemporaryFolder

public static File createTemporaryFolder(String prefix,
                                         String suffix,
                                         File directory)
                                  throws IOException
Creates a new temporary folder. It must be manually deleted after use. For details refer to Javadoc of File.createTempFile(String, String, File).

Parameters:
prefix - The prefix string to be used in generating the file's name; must be at least three characters long
suffix - The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used
directory - The directory in which the file is to be created, or null if the default temporary-file directory is to be used
Returns:
Newly created folder. Must be deleted manually if not needed anymore.
Throws:
IOException - Error during creation of folder.

writeFileContent

public static void writeFileContent(File file,
                                    byte[] fileContent)
                             throws IOException
Writes bytes into a file.

Parameters:
file - The file.
fileContent - The bytes.
Throws:
IOException - The Exception is thrown if the file could not be accessed

getFileAsByteArray

public static byte[] getFileAsByteArray(File file)
                                 throws IOException
Gets a file as byte array.

Parameters:
file - The file.
Returns:
The file bytes.
Throws:
IOException - if an IO error occurs.

getFileAsByteArray

public static byte[] getFileAsByteArray(String filename)
                                 throws IOException
Gets a file as byte array.

Parameters:
filename - The file name.
Returns:
The file bytes.
Throws:
IOException - if an IO error occurs.

getStreamAsByteArray

public static byte[] getStreamAsByteArray(InputStream in)
                                   throws IOException
Copies the content of the stream into a byte array. Use with caution and only when the stream contains a limited amount of bytes (smaller than 32 MB). This method is inefficient if the amount of bytes in the stream can not be queried in advance.

Parameters:
in - the input stream
Returns:
the byte array
Throws:
IOException - if the stream contains too many bytes, or if an I/O error occurs

getFileAsString

public static String getFileAsString(URL url)
                              throws IOException
Gets the complete content of a file as string.

Parameters:
url - The file URL.
Returns:
The file content.
Throws:
IOException - The Exception is thrown if the file could not be accessed

createAndFillFile

public static void createAndFillFile(File file,
                                     String content)
                              throws IOException
Creates and fills a file with the specified content, using the default charset.

Parameters:
file - The file.
content - The content.
Throws:
IOException - The Exception is thrown if the file could not be accessed

createAndFillFile

public static void createAndFillFile(File file,
                                     String content,
                                     Charset charset)
                              throws IOException
Creates and fills a file with the specified content using the given charset.

Parameters:
file - The file.
content - The content.
charset - The file should be written using this charset.
Throws:
IOException - The Exception is thrown if the file could not be accessed

createAndFillFileAndDir

public static void createAndFillFileAndDir(File file,
                                           String content)
                                    throws IOException
Creates and fills a file with the specified content. Creates the directory path if it is not already existing.

Parameters:
file - The file.
content - The content.
Throws:
IOException - The Exception is thrown if the file could not be accessed

createAndFillFileAndDir

public static void createAndFillFileAndDir(File file,
                                           URL url)
                                    throws IOException
Creates and fills a file with the specified content. Creates the directory path if it is not already existing.

Parameters:
file - The file.
url - The URL the content has to be read from.
Throws:
IOException - The Exception is thrown if the file could not be accessed

getVendorFromManifest

public static String getVendorFromManifest(File jarFile)
Gets the vendor string from a jar file.

Parameters:
jarFile - The jar file.
Returns:
The vendor. Returns an empty string if vendor was not read.

combinePaths

public static String combinePaths(String... paths)
Puts the given path fragments together.

Parameters:
paths - The path fragments.
Returns:
The combined path.

getDirectoryNames

public static List<String> getDirectoryNames(File rootDirectory)
Gets a list of all subdirectories of the given root directory.

Parameters:
rootDirectory - The root directory.
Returns:
A list of all subdirectories.

areBinaryEqual

public static boolean areBinaryEqual(File file1,
                                     File file2)
                              throws IOException
Checks whether the two files are binary equal. offset.

Parameters:
file1 - First file
file2 - Second file
Returns:
true, if and only if the two files are binary equal
Throws:
IOException - The Exception is thrown if the file could not be accessed

areBinaryEqual

public static boolean areBinaryEqual(File file1,
                                     File file2,
                                     int offset)
                              throws IOException
Checks whether the two files are binary equal starting with a given offset. If two files have the same length and the length is smaller than the given offset, they are considered equal.

Parameters:
file1 - First file
file2 - Second file
offset - Byte offset
Returns:
true, if and only if the two files are binary equal starting at the offset
Throws:
IOException - The Exception is thrown if the file could not be accessed

readStringsFromFile

public static List<String> readStringsFromFile(File inputFile)
                                        throws IOException
Reads the lines from a UTF-8 encoded text file into a list of strings, each line into one string.

Parameters:
inputFile - the input text file
Returns:
a newly created list of strings
Throws:
IOException - an I/O error occurred

writeStringsToFile

public static void writeStringsToFile(File file,
                                      List<String> lines)
                               throws IOException
Writes the given strings one per line into a UTF-8 encoded text file. If the file exists, it will be overwritten. If the file does not exist, it will be created.

Parameters:
file - the text file
lines - the text lines
Throws:
IOException - an I/O error occurred

createUniqueFile

public static File createUniqueFile(String prefix,
                                    String suffix,
                                    File directory)
                             throws IOException
Creates a new file with the given prefix and suffix in the given directory. If a file with that name already exists, a dash ("-") and a number (starting with 1) will be inserted between prefix and suffix.

Parameters:
prefix - the file prefix
suffix - the file suffix. May be null.
directory - the directory where to create the file
Returns:
the newly created file
Throws:
IOException - if an I/O error occurred

getFileExtension

public static String getFileExtension(File file)
Gets the extension of the file. The separating '.' is not contained.

Parameters:
file - file to get extension for.
Returns:
the extension of the file

getFilenameWithoutExtension

public static String getFilenameWithoutExtension(File file)
Gets the name of the file without the extension.

Parameters:
file - the file to get the filename for.
Returns:
the filename without the extension.


Copyright © 2019. All rights reserved.