Class FileUtil


  • public final class FileUtil
    extends java.lang.Object
    Various static helper methods to deal with files and file names.
    Since:
    0.2
    Author:
    Erich Schubert
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private FileUtil()
      Fake Constructor.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean exists​(java.net.URI file)
      Check if the file/resource identified by an URI exists.
      static java.lang.String getFilenameExtension​(java.lang.String name)
      Returns the lower case extension of the selected file.
      static java.lang.String getFilenameExtension​(java.nio.file.Path file)
      Returns the lower case extension of the selected file.
      static java.io.File locateFile​(java.lang.String name, java.lang.String basedir)
      Try to locate an file in the filesystem, given a partial name and a prefix.
      static java.io.InputStream open​(java.net.URI file, java.nio.file.OpenOption... opts)
      Open a file identified by an URI for reading.
      static java.io.InputStream openSystemFile​(java.lang.String filename)
      Try to open a file, first trying the file system, then falling back to the classpath.
      static java.lang.String slurp​(java.io.InputStream is)
      Load an input stream (e.g., a Java resource) into a String buffer.
      static java.io.InputStream tryGzipInput​(java.io.InputStream in)
      Try to open a stream as gzip, if it starts with the gzip magic.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileUtil

        private FileUtil()
        Fake Constructor. Use static methods.
    • Method Detail

      • getFilenameExtension

        public static java.lang.String getFilenameExtension​(java.nio.file.Path file)
        Returns the lower case extension of the selected file.

        If no file is selected, null is returned.

        Parameters:
        file - File object
        Returns:
        Returns the extension of the selected file in lower case or null
      • getFilenameExtension

        public static java.lang.String getFilenameExtension​(java.lang.String name)
        Returns the lower case extension of the selected file.

        If no file is selected, null is returned.

        Parameters:
        name - File name
        Returns:
        Returns the extension of the selected file in lower case or null
      • open

        public static java.io.InputStream open​(java.net.URI file,
                                               java.nio.file.OpenOption... opts)
                                        throws java.io.IOException
        Open a file identified by an URI for reading.
        Parameters:
        file - File
        opts - Open options
        Returns:
        File input stream
        Throws:
        java.io.IOException - on error
      • exists

        public static boolean exists​(java.net.URI file)
        Check if the file/resource identified by an URI exists.
        Parameters:
        file - File URI
        Returns:
        true if the file exists.
      • openSystemFile

        public static java.io.InputStream openSystemFile​(java.lang.String filename)
                                                  throws java.io.IOException
        Try to open a file, first trying the file system, then falling back to the classpath.
        Parameters:
        filename - File name in system notation
        Returns:
        Input stream
        Throws:
        java.io.IOException
      • tryGzipInput

        public static java.io.InputStream tryGzipInput​(java.io.InputStream in)
                                                throws java.io.IOException
        Try to open a stream as gzip, if it starts with the gzip magic.
        Parameters:
        in - original input stream
        Returns:
        old input stream or a GZIPInputStream if appropriate.
        Throws:
        java.io.IOException - on IO error
      • locateFile

        public static java.io.File locateFile​(java.lang.String name,
                                              java.lang.String basedir)
        Try to locate an file in the filesystem, given a partial name and a prefix.
        Parameters:
        name - file name
        basedir - extra base directory to try
        Returns:
        file, if the file could be found. null otherwise
      • slurp

        public static java.lang.String slurp​(java.io.InputStream is)
                                      throws java.io.IOException
        Load an input stream (e.g., a Java resource) into a String buffer. The stream is closed afterwards.
        Parameters:
        is - Input stream
        Returns:
        String with file/resource contents.
        Throws:
        java.io.IOException - on IO errors