Class FileUtil

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

public class FileUtil extends Object
Some convenient file methods.
  • Method Details

    • 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
    • getMappedImagePath

      public static String getMappedImagePath(String path, boolean inverted)
      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 charsetName) throws IOException
      Load a resource from an input stream (file, etc.). for mapping bytes to chars.
      Parameters:
      is - an input stream
      charsetName - the name of the charset to use
      Returns:
      the content as string
      Throws:
      IOException
    • loadResourceFromStream

      public static String loadResourceFromStream(InputStream is, Charset charset) throws IOException
      Load a resource from an input stream (file, etc.). for mapping bytes to chars.
      Parameters:
      is - an input stream
      charset - the charset to use
      Returns:
      the content as string
      Throws:
      IOException
    • loadApplicationResource

      public static Document loadApplicationResource(String resource) throws 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:
      JDOMException
      IOException
    • 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 IOException
      Copy a file.
      Parameters:
      src - the source file
      dest - the destination file
      Throws:
      IOException
    • copyStreams

      public static void copyStreams(InputStream is, OutputStream os) throws IOException
      Copies the content of the passed input stream into the passed output stream
      Parameters:
      is - the stream holding the content
      os - the stream into which the content should be written
      Throws:
      IOException
    • copyToDir

      public static File copyToDir(File src, File dir) throws 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
      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

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

      public static String getNoCommentContent(File file) throws Exception
      Throws:
      Exception
    • getContent

      @Deprecated public static String getContent(String filename) throws IOException
      Deprecated.
      use getContent(File) instead
      Throws:
      IOException
    • deleteDir

      @Deprecated public static void deleteDir(String dirPath)
      Deprecated.
      use deleteDir(File) instead
      Deletes the directory 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 directory and all its subdirectories (and so on).
      Parameters:
      dir - the directory
    • delete

      public static void delete(File file)
      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 method deleteDir(File) instead.
      Parameters:
      file - the file to be deleted
    • getFileFromPath

      @Deprecated(forRemoval=true) public static File getFileFromPath(String path)
      Deprecated, for removal: This API element is subject to removal in a future version.
      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.
      1. the VAR directory
      2. the LOCAL directory
      3. the BASE directory
      Parameters:
      path - the path string
      Returns:
      the file object
    • getFileFromBasePath

      public static File getFileFromBasePath(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 method returns the path using the base directory as base for the relative path.
      Parameters:
      path - the path string
      Returns:
      the file object
    • getFileFromVarPath

      public static File getFileFromVarPath(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 method returns the path using the VAR directory as base for the relative path.
      Parameters:
      path - the path string
      Returns:
      the file object
    • getFileFromLocalPath

      public static File getFileFromLocalPath(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 method returns the path using the LOCAL directory as base for the relative path.
      Parameters:
      path - the path string
      Returns:
      the file object
    • getFile

      public static File getFile(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 method returns the path using '.' as base for the relative path.
      Parameters:
      path - the path string
      Returns:
      the file object
    • getRelativeNormalizedPath

      public static String getRelativeNormalizedPath(File file)
      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

      public static String getRelativeNormalizedPath(File baseDir, File file)
      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

      public static String getAbsoluteNormalizedPath(File file)
      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

      public static String getAbsoluteNormalizedPath(File baseDir, File file)
      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 directory
      file - the file
      Returns:
      the absolute normalized path of the file.
    • 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)
    • resizeImage

      public static byte[] resizeImage(byte[] content, int maxWidth, int maxHeight)
    • extractZip

      public static void extractZip(File zip, File dir, String... files) throws IOException
      Exctract the given zip into a directory.
      Parameters:
      zip - the zip file
      dir - the destination directory
      files - a list of filenames, that should be excluded from extraction
      Throws:
      IOException