com.groiss.util
Class FileUtil

java.lang.Object
  extended by com.groiss.util.FileUtil

public class FileUtil
extends java.lang.Object

Some convenient file methods.


Constructor Summary
FileUtil()
           
 
Method Summary
static void copyFileOrDirectory(java.io.File src, java.io.File dest)
          copies a file or a whole folder to the given destination
static void copyTo(java.io.File src, java.io.File dest)
          Copy a file.
static java.io.File copyToDir(java.io.File src, java.io.File dir)
          Copy file to directory
static void createJarMetaInf(java.util.jar.JarOutputStream jarOut, java.util.Map<java.lang.String,java.lang.String> properties)
           
static void deleteDir(java.io.File dir)
          Deletes the passed direcory and all its subdirectories (and so on).
static void deleteDir(java.lang.String dirPath)
          Deletes the direcory with the given path.
static byte[] getBytesFromStream(java.io.InputStream is)
          Load the content of an InputStream into a byte array.
static java.lang.String getContent(java.io.File file)
          Get the content of a file as string.
static java.lang.String getContent(java.lang.String filename)
           
static java.io.File getFileFromPath(java.lang.String path)
          Get a file object from a pathname.
protected static java.lang.String getLogPrefix(int level)
           
static java.io.PrintWriter getLogWriter(java.lang.String fileName, int maxLogs)
           
static java.lang.String getMimeType(java.lang.String ext)
          Return the mime-type from the extension.
static java.lang.String getNoComment(java.io.InputStream in)
           
static java.lang.String getNoComment(java.io.Reader in)
          Get the contents of an input stream without the comments.
static java.lang.String getNoCommentContent(java.lang.String filename)
           
static java.lang.String getShinyPathMapping(java.lang.String path)
          Returns the shiny path for the passed resource path.
static boolean isTextFile(java.lang.String pathname)
          Returns true if the mime-time of this file starts with text
static java.util.List<java.io.File> listFiles(java.lang.String namePattern, java.io.File dir)
          List file with name specified with wildcard in directory and all its subdirectories.
static java.util.List<java.io.File> listFiles(java.lang.String namePattern, java.io.File dir, boolean recursive)
          List file with name specified with wildcard in directory.
static org.jdom.Document loadApplicationResource(java.lang.String resource)
          Load a resource, i.e.
static java.lang.String loadResource(java.lang.String resource)
          Load a resource as string from the classpath.
static java.lang.String loadResource(java.net.URL url)
          Load a resource as string from an url.
static byte[] loadResourceBytes(java.lang.String resource)
           
static byte[] loadResourceBytes(java.net.URL url)
          Load a resource as byte array from the classpath.
static java.lang.String loadResourceFromStream(java.io.InputStream is)
          Load a resource from an input stream (file, etc.).
static java.lang.String loadResourceFromStream(java.io.InputStream is, java.lang.String charset)
           
static void log(java.io.PrintWriter pw, java.lang.String str)
           
static void packJarFiles(java.util.jar.JarOutputStream jarOut, java.io.File dir, java.lang.String[] types)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtil

public FileUtil()
Method Detail

getMimeType

public static java.lang.String getMimeType(java.lang.String ext)
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

public static java.lang.String getShinyPathMapping(java.lang.String path)
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

public static java.lang.String loadResource(java.lang.String resource)
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

public static java.lang.String loadResource(java.net.URL url)
Load a resource as string from an url.

Parameters:
url - the url of the resource
Returns:
the content of the resource

loadResourceFromStream

public static java.lang.String loadResourceFromStream(java.io.InputStream is)
                                               throws java.io.IOException
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:
java.io.IOException

loadResourceFromStream

public static java.lang.String loadResourceFromStream(java.io.InputStream is,
                                                      java.lang.String charset)
                                               throws java.io.IOException
Throws:
java.io.IOException

loadApplicationResource

public static org.jdom.Document loadApplicationResource(java.lang.String resource)
                                                 throws java.io.FileNotFoundException,
                                                        org.jdom.JDOMException,
                                                        java.io.IOException
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:
java.io.FileNotFoundException
java.io.IOException
org.jdom.JDOMException

loadResourceBytes

public static byte[] loadResourceBytes(java.net.URL url)
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

public static byte[] loadResourceBytes(java.lang.String resource)

getBytesFromStream

public static byte[] getBytesFromStream(java.io.InputStream is)
Load the content of an InputStream into a byte array. The stream is closed afterwards.


copyTo

public static void copyTo(java.io.File src,
                          java.io.File dest)
                   throws java.io.FileNotFoundException,
                          java.io.IOException
Copy a file.

Parameters:
src - the source file
dest - the destination file
Throws:
java.io.FileNotFoundException
java.io.IOException

copyToDir

public static java.io.File copyToDir(java.io.File src,
                                     java.io.File dir)
                              throws java.io.FileNotFoundException,
                                     java.io.IOException
Copy file to directory

Parameters:
src -
dir -
Throws:
java.io.FileNotFoundException
java.io.IOException

copyFileOrDirectory

public static void copyFileOrDirectory(java.io.File src,
                                       java.io.File dest)
                                throws java.io.IOException
copies a file or a whole folder to the given destination

Parameters:
src - source location
dest - destination location
Throws:
java.io.IOException

getContent

public static java.lang.String getContent(java.io.File file)
                                   throws java.io.IOException
Get the content of a file as string.

Parameters:
file - the file
Returns:
the content
Throws:
java.io.IOException

getNoComment

public static java.lang.String getNoComment(java.io.InputStream in)
                                     throws java.lang.Exception
Throws:
java.lang.Exception

getNoComment

public static java.lang.String getNoComment(java.io.Reader in)
                                     throws java.lang.Exception
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:
java.lang.Exception

getNoCommentContent

public static java.lang.String getNoCommentContent(java.lang.String filename)
                                            throws java.lang.Exception
Throws:
java.lang.Exception

getContent

public static java.lang.String getContent(java.lang.String filename)
                                   throws java.io.IOException
Throws:
java.io.IOException

deleteDir

public static void deleteDir(java.lang.String dirPath)
Deletes the direcory with the given path. All subdirectories (and so on) will be also deleted.

Parameters:
dirPath - the directory path

deleteDir

public static void deleteDir(java.io.File dir)
Deletes the passed direcory and all its subdirectories (and so on). If the passed parameter is a file and not a directory it will not be deleted

Parameters:
dir - the directory

getFileFromPath

public static java.io.File getFileFromPath(java.lang.String path)
Get a file object from a pathname. If the path is absolute, the method returns the corresponding file. If the path is relative, the moethod returns the path using the base directory from settings as base for the relative path.

Parameters:
path - the path string
Returns:
the file object

listFiles

public static java.util.List<java.io.File> listFiles(java.lang.String namePattern,
                                                     java.io.File dir)
List file with name specified with wildcard in directory and all its subdirectories.

Parameters:
namePattern -
dir -
Returns:
list of found File objects

listFiles

public static java.util.List<java.io.File> listFiles(java.lang.String namePattern,
                                                     java.io.File dir,
                                                     boolean recursive)
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

public static boolean isTextFile(java.lang.String pathname)
Returns true if the mime-time of this file starts with text


createJarMetaInf

public static void createJarMetaInf(java.util.jar.JarOutputStream jarOut,
                                    java.util.Map<java.lang.String,java.lang.String> properties)
                             throws java.io.IOException
Throws:
java.io.IOException

packJarFiles

public static void packJarFiles(java.util.jar.JarOutputStream jarOut,
                                java.io.File dir,
                                java.lang.String[] types)

getLogPrefix

protected static java.lang.String getLogPrefix(int level)

log

public static void log(java.io.PrintWriter pw,
                       java.lang.String str)

getLogWriter

public static java.io.PrintWriter getLogWriter(java.lang.String fileName,
                                               int maxLogs)
                                        throws java.lang.Exception
Throws:
java.lang.Exception


@enterprise 8.0.22989 Copyright © 2001-2017 Groiss Informatics GmbH. All Rights Reserved.