Package com.groiss.util
Class FileUtil
java.lang.Object
com.groiss.util.FileUtil
Some convenient file methods.
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
copyFileOrDirectory
(File src, File dest) copies a file or a whole folder to the given destinationstatic void
copyStreams
(InputStream is, OutputStream os) Copies the content of the passed input stream into the passed output streamstatic void
Copy a file.static File
Copy file to directorystatic void
createJarMetaInf
(JarOutputStream jarOut, Map<String, String> properties) static void
Deletes the passed file silently, i.e. any exception thrown when doing so will be caught and logged and the file will be registered to be deleted on exit.static void
Deletes the passed directory and all its subdirectories (and so on).static void
Deprecated.static void
extractZip
(File zip, File dir, String... files) Exctract the given zip into a directory.static String
Gets the absolute normalized path of a file.static String
getAbsoluteNormalizedPath
(File baseDir, File file) Gets the absolute normalized path of a file.static byte[]
Load the content of an InputStream into a byte array.static String
getContent
(File file) Get the content of a file as string.static String
getContent
(String filename) Deprecated.usegetContent(File)
insteadstatic File
Get a file object from a pathname.static File
getFileFromBasePath
(String path) Get a file object from a pathname.static File
getFileFromLocalPath
(String path) Get a file object from a pathname.static File
getFileFromPath
(String path) Deprecated, for removal: This API element is subject to removal in a future version.use one of the search roots specific methodsgetFileFromVarPath(String)
,getFileFromLocalPath(String)
orgetFileFromBasePath(String)
static File
getFileFromVarPath
(String path) Get a file object from a pathname.static String
getMappedImagePath
(String path, boolean inverted) Returns the shiny path for the passed resource path.static String
getMimeType
(String ext) Return the mime-type from the extension.static String
Get the contents of an input stream without the comments.static String
getNoComment
(Reader in) Get the contents of a reader without the comments.static String
getNoCommentContent
(File file) static String
getNoCommentContent
(String filename) Deprecated, for removal: This API element is subject to removal in a future version.usegetNoCommentContent(File)
insteadstatic String
Gets the relative normalized path of a file with respect to the configured base directory (Settings.getBaseDir()
).static String
getRelativeNormalizedPath
(File baseDir, File file) Gets the relative normalized path of a file with respect to a specified base directory.static String
getShinyPathMapping
(String path) Returns the shiny path for the passed resource path.static boolean
isTextFile
(String pathname) Returns true if the mime-time of this file starts with textList file with name specified with wildcard in directory and all its subdirectories.List file with name specified with wildcard in directory.static Document
loadApplicationResource
(String resource) Load a resource, i.e. a file from the classpath.static String
loadResource
(String resource) Load a resource as string from the classpath.static String
loadResource
(URL url) Load a resource as string from an url.static byte[]
loadResourceBytes
(String resource) static byte[]
loadResourceBytes
(URL url) Load a resource as byte array from the classpath.static String
Load a resource from an input stream (file, etc.).static String
loadResourceFromStream
(InputStream is, String charsetName) Load a resource from an input stream (file, etc.).static String
loadResourceFromStream
(InputStream is, Charset charset) Load a resource from an input stream (file, etc.).static void
packJarFiles
(JarOutputStream jarOut, File dir, String[] types) static byte[]
resizeImage
(byte[] content, int maxWidth, int maxHeight)
-
Method Details
-
getMimeType
Return the mime-type from the extension. The available mime-types are defined in the file mime.types in the conf directory.- Parameters:
ext
- the extension, for example "doc" or "html"- Returns:
- the mime-type, for example "text/html"
-
getShinyPathMapping
Returns the shiny path for the passed resource path. If no shiny path is available the passed path is returned.- Parameters:
path
- the original path of a resource- Returns:
- the path for the shiny version of the resource
-
getMappedImagePath
Returns the shiny path for the passed resource path. If no shiny path is available the passed path is returned.- Parameters:
path
- the original path of a resource- Returns:
- the path for the shiny version of the resource
-
loadResource
Load a resource as string from the classpath. The class loader defined in Settings is used.- Parameters:
resource
- a path to a resource- Returns:
- the content of the resource
-
loadResource
Load a resource as string from an url.- Parameters:
url
- the url of the resource- Returns:
- the content of the resource
-
loadResourceFromStream
Load a resource from an input stream (file, etc.). We use the default encoding (file.encoding) for mapping bytes to chars.- Parameters:
is
- an input stream- Returns:
- the content as string
- Throws:
IOException
-
loadResourceFromStream
Load a resource from an input stream (file, etc.). for mapping bytes to chars.- Parameters:
is
- an input streamcharsetName
- the name of the charset to use- Returns:
- the content as string
- Throws:
IOException
-
loadResourceFromStream
Load a resource from an input stream (file, etc.). for mapping bytes to chars.- Parameters:
is
- an input streamcharset
- the charset to use- Returns:
- the content as string
- Throws:
IOException
-
loadApplicationResource
Load a resource, i.e. a file from the classpath. The system tries to find out the application the resource belongs to and localizes it with this resource. The resource can only be found if it lays in the application directory in a jar file in the lib folder or in the class folder.- Parameters:
resource
- a path in the classpath- Returns:
- the localized string
- Throws:
JDOMException
IOException
-
loadResourceBytes
Load a resource as byte array from the classpath. The class loader defined in Settings is used.- Parameters:
url
- the url of a resource- Returns:
- the content of the resource
-
loadResourceBytes
-
getBytesFromStream
Load the content of an InputStream into a byte array. The stream is closed afterwards. -
copyTo
Copy a file.- Parameters:
src
- the source filedest
- the destination file- Throws:
IOException
-
copyStreams
Copies the content of the passed input stream into the passed output stream- Parameters:
is
- the stream holding the contentos
- the stream into which the content should be written- Throws:
IOException
-
copyToDir
Copy file to directory- Parameters:
src
-dir
-- Throws:
FileNotFoundException
IOException
-
copyFileOrDirectory
copies a file or a whole folder to the given destination- Parameters:
src
- source locationdest
- destination location- Throws:
IOException
-
getContent
Get the content of a file as string.- Parameters:
file
- the file- Returns:
- the content
- Throws:
IOException
-
getNoComment
Get the contents of an input stream without the comments. The method filters out the comments starting with "/*" and ending with "*/"- Parameters:
in
- the input stream- Returns:
- the content
- Throws:
Exception
-
getNoComment
Get the contents of a reader without the comments. The method filters out the comments starting with "/*" and ending with "*/"- Parameters:
in
- the reader- Returns:
- the content
- Throws:
Exception
-
getNoCommentContent
@Deprecated(forRemoval=true) public static String getNoCommentContent(String filename) throws Exception Deprecated, for removal: This API element is subject to removal in a future version.usegetNoCommentContent(File)
instead- Throws:
Exception
-
getNoCommentContent
- Throws:
Exception
-
getContent
Deprecated.usegetContent(File)
instead- Throws:
IOException
-
deleteDir
Deprecated.usedeleteDir(File)
insteadDeletes the directory with the given path. All subdirectories (and so on) will be also deleted.- Parameters:
dirPath
- the directory path
-
deleteDir
Deletes the passed directory and all its subdirectories (and so on).- Parameters:
dir
- the directory
-
delete
Deletes the passed file silently, i.e. any exception thrown when doing so will be caught and logged and the file will be registered to be deleted on exit. Note: in case of directories this method can only delete empty directories. To delete a directory and its content use methoddeleteDir(File)
instead.- Parameters:
file
- the file to be deleted
-
getFileFromPath
Deprecated, for removal: This API element is subject to removal in a future version.use one of the search roots specific methodsgetFileFromVarPath(String)
,getFileFromLocalPath(String)
orgetFileFromBasePath(String)
Get a file object from a pathname. If the path is absolute, the method returns the corresponding file. If the path is relative, the method returns the path using the following directories in the given order as base for the relative path.- the VAR directory
- the LOCAL directory
- the BASE directory
- Parameters:
path
- the path string- Returns:
- the file object
-
getFileFromBasePath
Get a file object from a pathname. If the path is absolute, the method returns the corresponding file. If the path is relative, the method returns the path using the base directory as base for the relative path.- Parameters:
path
- the path string- Returns:
- the file object
-
getFileFromVarPath
Get a file object from a pathname. If the path is absolute, the method returns the corresponding file. If the path is relative, the method returns the path using the VAR directory as base for the relative path.- Parameters:
path
- the path string- Returns:
- the file object
-
getFileFromLocalPath
Get a file object from a pathname. If the path is absolute, the method returns the corresponding file. If the path is relative, the method returns the path using the LOCAL directory as base for the relative path.- Parameters:
path
- the path string- Returns:
- the file object
-
getFile
Get a file object from a pathname. If the path is absolute, the method returns the corresponding file. If the path is relative, the method returns the path using '.' as base for the relative path.- Parameters:
path
- the path string- Returns:
- the file object
-
getRelativeNormalizedPath
Gets the relative normalized path of a file with respect to the configured base directory (Settings.getBaseDir()
). If the file is not related to the base directory, the files absolute normalized path is returned.- Parameters:
file
- the file- Returns:
- the relative normalized path of the file with respect to the configured base directory.
-
getRelativeNormalizedPath
Gets the relative normalized path of a file with respect to a specified base directory. If the file is not related to the base file, the files absolute normalized path is returned.- Parameters:
baseDir
- the base directory to relativize (Path.relativize(Path)
)the file to.file
- the file- Returns:
- the relative normalized path of the file with respect to the base directory.
-
getAbsoluteNormalizedPath
Gets the absolute normalized path of a file. If the given file is relative, it is assumed to be relative to the configured base directory (Settings.getBaseDir()
).- Parameters:
file
- the file- Returns:
-
getAbsoluteNormalizedPath
Gets the absolute normalized path of a file. If the given file is relative, it is assumed to be relative to the given base directory.- Parameters:
baseDir
- the base directoryfile
- the file- Returns:
- the absolute normalized path of the file.
-
listFiles
List file with name specified with wildcard in directory and all its subdirectories.- Parameters:
namePattern
-dir
-- Returns:
- list of found File objects
-
listFiles
List file with name specified with wildcard in directory. If parameter recursive is true all its subdirectories are also investigated.- Parameters:
namePattern
-dir
-recursive
-- Returns:
- list of found File objects
-
isTextFile
Returns true if the mime-time of this file starts with text -
createJarMetaInf
public static void createJarMetaInf(JarOutputStream jarOut, Map<String, String> properties) throws IOException- Throws:
IOException
-
packJarFiles
-
resizeImage
public static byte[] resizeImage(byte[] content, int maxWidth, int maxHeight) -
extractZip
Exctract the given zip into a directory.- Parameters:
zip
- the zip filedir
- the destination directoryfiles
- a list of filenames, that should be excluded from extraction- Throws:
IOException
-
deleteDir(File)
instead