com.groiss.util
Class FileUtil

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

public class FileUtil
extends Object

Some convenient file methods.


Constructor Summary
FileUtil()
           
 
Method Summary
static void copyFileOrDirectory(File src, File dest)
          copies a file or a whole folder to the given destination
static void copyTo(File src, File dest)
          Copy a file.
static File copyToDir(File src, File dir)
          Copy file to directory
static void createJarMetaInf(JarOutputStream jarOut, Map<String,String> properties)
           
static void deleteDir(File dir)
          Deletes the passed direcory and all its subdirectories (and so on).
static void deleteDir(String dirPath)
          Deletes the direcory with the given path.
static byte[] getBytesFromStream(InputStream is)
          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)
           
static File getFileFromPath(String path)
          Get a file object from a pathname.
static String getMimeType(String ext)
          Return the mime-type from the extension.
static String getNoComment(InputStream in)
           
static String getNoComment(Reader in)
          Get the contents of an input stream without the comments.
static String getNoCommentContent(String filename)
           
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 text
static List<File> listFiles(String namePattern, File dir)
          List file with name specified with wildcard in directory and all its subdirectories.
static List<File> listFiles(String namePattern, File dir, boolean recursive)
          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 loadResourceFromStream(InputStream is)
          Load a resource from an input stream (file, etc.).
static String loadResourceFromStream(InputStream is, String charset)
           
static void packJarFiles(JarOutputStream jarOut, File dir, 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 String getMimeType(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 String getShinyPathMapping(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 String loadResource(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 String loadResource(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 String loadResourceFromStream(InputStream is)
                                     throws 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:
IOException

loadResourceFromStream

public static String loadResourceFromStream(InputStream is,
                                            String charset)
                                     throws IOException
Throws:
IOException

loadApplicationResource

public static Document loadApplicationResource(String resource)
                                        throws FileNotFoundException,
                                               JDOMException,
                                               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:
FileNotFoundException
IOException
JDOMException

loadResourceBytes

public static byte[] loadResourceBytes(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(String resource)

getBytesFromStream

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


copyTo

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

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

copyToDir

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

Parameters:
src -
dir -
Throws:
FileNotFoundException
IOException

copyFileOrDirectory

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

Parameters:
src - source location
dest - destination location
Throws:
IOException

getContent

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

Parameters:
file - the file
Returns:
the content
Throws:
IOException

getNoComment

public static String getNoComment(InputStream in)
                           throws Exception
Throws:
Exception

getNoComment

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

getNoCommentContent

public static String getNoCommentContent(String filename)
                                  throws Exception
Throws:
Exception

getContent

public static String getContent(String filename)
                         throws IOException
Throws:
IOException

deleteDir

public static void deleteDir(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(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 File getFileFromPath(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 List<File> listFiles(String namePattern,
                                   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 List<File> listFiles(String namePattern,
                                   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(String pathname)
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

public static void packJarFiles(JarOutputStream jarOut,
                                File dir,
                                String[] types)


@enterprise 8.0.10078 Copyright © 2001-2011 Groiss Informatics GmbH. All Rights Reserved.