Class FileUtil


  • public class FileUtil
    extends Object
    File-related utility functions.
    • Constructor Detail

      • FileUtil

        public FileUtil()
    • Method Detail

      • getContents

        public static List<String> getContents​(String filename)
                                        throws IOException
        Gets the content of filename as a list of lines.
        Parameters:
        filename - name of the input file
        Returns:
        a list of strings representing the file's lines
        Throws:
        IOException - if an IOException occurred
      • getContents

        public static List<String> getContents​(String filename,
                                               String encoding)
                                        throws IOException
        Gets the content of filename as a list of lines.
        Parameters:
        filename - name of the input file
        encoding - charset to use to decode filename
        Returns:
        a list of strings representing the file's lines
        Throws:
        IOException - if an Exception occurred
      • getContents

        public static List<String> getContents​(URI uri)
                                        throws IOException
        Gets the content a compressed file passed as an URI.
        Parameters:
        uri - uri of the input resource
        Returns:
        a list of strings representing the compressed file's lines
        Throws:
        IOException - if an Exception occurred
      • getContents

        public static List<String> getContents​(InputStream inputStream)
                                        throws IOException
        Gets the content of an inputSteam as a list of lines.
        Parameters:
        inputStream - inputStream to read from
        Returns:
        a list of lines
        Throws:
        IOException - if an Exception occurred
      • getContents

        public static List<String> getContents​(InputStream inputStream,
                                               String encoding)
                                        throws IOException
        Gets the content of an inputSteam as a list of lines. inputStream is decoded with the specified charset.
        Parameters:
        inputStream - inputStream to read from
        encoding - encoding of the inputStream
        Returns:
        a list of lines
        Throws:
        IOException - if an Exception occurred
      • setContents

        public static void setContents​(String filename,
                                       String contents)
                                throws IOException
        Saves the String to a file with the given filename.
        Parameters:
        filename - the pathname of the file to create (or overwrite)
        contents - the data to save
        Throws:
        IOException - if an I/O error occurs.
      • setContents

        public static void setContents​(String filename,
                                       List<String> lines)
                                throws IOException
        Saves the List of Strings to a file with the given filename.
        Parameters:
        filename - the pathname of the file to create (or overwrite)
        lines - the Strings to save as lines in the file
        Throws:
        IOException - if an I/O error occurs.
      • setContents

        public static void setContents​(String filename,
                                       String contents,
                                       String encoding)
                                throws IOException
        Saves lines into a file named filename, using encoding charset.
        Parameters:
        filename - the pathname of the file to create (or overwrite)
        contents - the data to save
        Throws:
        IOException - if an I/O error occurs.
      • setContents

        public static void setContents​(String filename,
                                       List<String> lines,
                                       String encoding)
                                throws IOException
        Saves lines into a file named filename, using encoding charset.
        Parameters:
        filename - the pathname of the file to create (or overwrite)
        lines - the data to save
        Throws:
        IOException - if an I/O error occurs.
      • getExtension

        public static String getExtension​(String s)
      • getExtension

        public static String getExtension​(File f)
      • addExtensionIfNone

        public static File addExtensionIfNone​(File file,
                                              String extension)
      • removeExtensionIfAny

        public static File removeExtensionIfAny​(File file)
      • getFileNameFromLayerName

        public static String getFileNameFromLayerName​(String layerName)
      • close

        public static void close​(Closeable is)
      • copyInputStreamToTempFile

        public static File copyInputStreamToTempFile​(InputStream in,
                                                     String prefix,
                                                     String suffix,
                                                     TaskMonitor monitor)
                                              throws IOException
        utility method to copy an inputstream to a temp file for further processing NOTE: prefix, suffix, monitor are optional and may be {@link null}
        Parameters:
        in - inputSteam to copy
        prefix - file name prefix - default "openjump"
        suffix - file name suffix - default ".tmp"
        monitor - to get feedback during the stream reading
        Returns:
        the temp file
        Throws:
        IOException - if an Exception occurred
      • findFileInClassPath

        public static File findFileInClassPath​(String fileOrFolder)
        utility method to find files or folders in classpath easy without handling exceptions or such returns NULL if none could b found
        Returns:
        file object or null