Package org.europa.together.utils
Class FileUtils
java.lang.Object
org.europa.together.utils.FileUtils
Some helpful stuff for file handling.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendFile(String filePath, String content) Append content to a given text file at the end of the file in UTF-8.static voidCopy a single File from a source to a destination.static longgetFileSize(String filePath, String dimension) Get the bytes of file.static byte[]Convert an InputStream to an ByteArray.listFileTree(File directory) Discover a directory and all subdirectories to collect their the files in a List.static StringreadFileFromClasspath(String path) Read a file from the class path.static StringreadFileStream(File file, ByteOrderMark... charset) Reads the full content of a text file in UTF-8.The second parameter charset is optional.static byte[]readRemoteResource(URL uri) Read a binary remote resource like Image or PDF from a remote source (http / https) as a File object.static byte[]toBytes(int... ints) Create from an HEX repersentation integers aa byte array.static booleanwriteStringToFile(String content, String destinationFile) Write a String to a File.It is possible to create an empty file whitout content.
-
Method Details
-
inputStreamToByteArray
Convert an InputStream to an ByteArray.- Parameters:
input- as InputStream- Returns:
- byte[] as Array
- Throws:
IOException- case of error
-
toBytes
public static byte[] toBytes(int... ints) Create from an HEX repersentation integers aa byte array.- Parameters:
ints- as Hex- Returns:
- byte as Array
-
writeStringToFile
Write a String to a File.It is possible to create an empty file whitout content. DestinationFile is a full qualitied path including the file name. Sample: /home/user/file.txt- Parameters:
content- as StringdestinationFile- as String- Returns:
- true on success
- Throws:
IOException- case of error
-
getFileSize
Get the bytes of file. It is also possible to choose a dimension for the file size. The following options for dimension are available: kilo, mega, giga and tera.- Parameters:
filePath- as Stringdimension- as String- Returns:
- fileSize as double
-
readFileFromClasspath
Read a file from the class path.- Parameters:
path- to the file- Returns:
- fielContent as String
- Throws:
FileNotFoundException- case of errorIOException
-
readRemoteResource
Read a binary remote resource like Image or PDF from a remote source (http / https) as a File object.- Parameters:
uri- as String- Returns:
- resource as File
- Throws:
MalformedURLException- case of errorIOException
-
readFileStream
public static String readFileStream(File file, ByteOrderMark... charset) throws FileNotFoundException, IOException Reads the full content of a text file in UTF-8.The second parameter charset is optional.- Parameters:
file- as Filecharset- as ByteOrderMark (Optional)- Returns:
- fileContent as String
- Throws:
FileNotFoundException- case of errorIOException- case of error
-
appendFile
Append content to a given text file at the end of the file in UTF-8.- Parameters:
filePath- as Stringcontent- as String- Throws:
IOException- case of error
-
copyFile
public static void copyFile(File source, File destination) throws FileNotFoundException, IOException Copy a single File from a source to a destination.- Parameters:
source- as Filedestination- as File- Throws:
FileNotFoundException- case of errorIOException- case of error
-
listFileTree
Discover a directory and all subdirectories to collect their the files in a List. The list entries are the full path, the filename with file extension. Empty directories will not listed.
Example /usr/home/john/file.txt- Parameters:
directory- as File- Returns:
- a Set of Files
-