public class FileHelper
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
CRLF
The system-dependent carriage return symbol
|
static java.lang.String |
SLASH
The system-dependent symbol for separating paths
|
| Constructor and Description |
|---|
FileHelper() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
commandExists(java.lang.String command)
Checks if a command exists in the system by attempting to run it
|
static void |
createIfNotExists(java.io.File f)
Creates a file and its parent directory if they do not exist
|
static boolean |
deleteFile(java.lang.String filename)
Deletes a file
|
static boolean |
fileExists(java.lang.String filename)
Checks whether a file exists in the filesystem
|
static java.util.List<java.lang.String> |
getResourceListing(java.lang.Class<?> clazz,
java.lang.String path,
java.lang.String glob)
List directory contents for a resource folder.
|
static java.util.List<java.lang.String> |
getResourceListing(java.lang.Class<?> clazz,
java.lang.String path,
java.lang.String glob,
java.lang.String exclude_glob)
List directory contents for a resource folder.
|
static boolean |
internalFileExists(java.lang.Class<?> c,
java.lang.String path)
Checks if an internal file exists
|
static byte[] |
internalFileToBytes(java.lang.Class<?> c,
java.lang.String path)
Gets an array of bytes from an internal file
|
static java.io.InputStream |
internalFileToStream(java.lang.Class<?> c,
java.lang.String path)
Gets an input stream on an internal file
|
static java.lang.String |
internalFileToString(java.lang.Class<?> c,
java.lang.String path)
Gets a string from an internal file
|
static java.lang.String |
internalFileToString(java.lang.Object o,
java.lang.String filename)
Gets a string from an internal file
|
static java.util.List<java.lang.String> |
listAllFiles(java.net.URL url,
java.lang.String glob)
List all files in a folder whose filename matches a regex pattern
|
static java.util.List<java.lang.String> |
listAllInternalFiles(java.lang.Class<?> clazz,
java.lang.String directory,
java.lang.String glob)
List all files in a folder whose filename matches a regex pattern
|
static byte[] |
mergePdf(java.lang.String dest,
java.lang.String... paths)
Merges multiple PDF files into a single file
|
static byte[] |
readToBytes(java.io.File f)
Reads the contents of a file and puts it into an array of bytes.
|
static java.lang.String |
readToString(java.io.File f)
Reads the contents of a file and puts it into a string.
|
static java.lang.String |
readToString(java.io.InputStream is)
Reads an input stream and puts its contents into a string
|
static java.lang.String |
replaceExtension(java.lang.String filename,
java.lang.String extension)
Replace the extension of a filename with another.
|
static java.lang.String |
streamToString(java.io.InputStream in)
Reads a file and puts its contents in a string
|
static java.lang.String |
trimExtension(java.lang.String filename)
Trims the extension of a filename.
|
static void |
writeFromBytes(java.io.File f,
byte[] bFile)
Writes to a file from an array of bytes
|
static void |
writeFromString(java.io.File f,
java.lang.String content)
Writes the content of a string to a file
|
public static final transient java.lang.String CRLF
public static final transient java.lang.String SLASH
public static java.lang.String readToString(java.io.File f)
f - The file to readpublic static java.lang.String readToString(java.io.InputStream is)
is - The input streampublic static byte[] readToBytes(java.io.File f)
f - The file to readpublic static void writeFromString(java.io.File f,
java.lang.String content)
f - The file to write to. If the file does not exist, it will be
createdcontent - The content to writepublic static void createIfNotExists(java.io.File f)
f - The file to createpublic static void writeFromBytes(java.io.File f,
byte[] bFile)
f - The file to write to. If the file does not exist, it will be
createdbFile - The content to writepublic static boolean deleteFile(java.lang.String filename)
filename - The filenamepublic static boolean fileExists(java.lang.String filename)
filename - The filename to look forpublic static java.lang.String replaceExtension(java.lang.String filename,
java.lang.String extension)
filename - The original filenameextension - The extension to replace withpublic static java.lang.String trimExtension(java.lang.String filename)
filename - The filenamepublic static java.lang.String internalFileToString(java.lang.Class<?> c,
java.lang.String path)
c - The class used as reference. The path is expressed relative to
the location of this class in the project.path - The pathnull if the path does not correspond
to a resourcepublic static byte[] internalFileToBytes(java.lang.Class<?> c,
java.lang.String path)
c - The class used as reference. The path is expressed relative to
the location of this class in the project.path - The pathnull if the path does not correspond
to a resourcepublic static java.io.InputStream internalFileToStream(java.lang.Class<?> c,
java.lang.String path)
c - The class used as reference. The path is expressed relative to
the location of this class in the project.path - The pathnull if the path does not correspond
to a resourcepublic static boolean internalFileExists(java.lang.Class<?> c,
java.lang.String path)
c - The reference classpath - The path of the filepublic static java.lang.String streamToString(java.io.InputStream in)
throws java.io.IOException
in - The input stream to readjava.io.IOException - If something bad occurspublic static boolean commandExists(java.lang.String command)
command - The command's namepublic static java.util.List<java.lang.String> listAllFiles(java.net.URL url,
java.lang.String glob)
url - The source folderglob - The pattern to matchpublic static java.util.List<java.lang.String> listAllInternalFiles(java.lang.Class<?> clazz,
java.lang.String directory,
java.lang.String glob)
clazz - A class referencedirectory - The source folderglob - The pattern to matchpublic static java.util.List<java.lang.String> getResourceListing(java.lang.Class<?> clazz,
java.lang.String path,
java.lang.String glob)
clazz - Any java class that lives in the same place as the resources you want.path - Should end with "/", but not start with one.glob - The pattern to matchpublic static java.util.List<java.lang.String> getResourceListing(java.lang.Class<?> clazz,
java.lang.String path,
java.lang.String glob,
java.lang.String exclude_glob)
clazz - Any java class that lives in the same place as the resources you want.path - Should end with "/", but not start with one.glob - The pattern to matchexclude_glob - The pattern to exclude filespublic static java.lang.String internalFileToString(java.lang.Object o,
java.lang.String filename)
o - The object used as reference. The path is expressed relative to
the location of the object's declaring class in the project.filename - The pathnull if the path does not correspond
to a resource or if o is nullpublic static byte[] mergePdf(java.lang.String dest,
java.lang.String... paths)
dest - The destination filenamepaths - The input filesCopyright © Sylvain HallĂ©. All Rights Reserved.