Class FileUtil


  • public final class FileUtil
    extends java.lang.Object
    Generic utilities related to file and stream handling.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void copyContents​(java.io.InputStream in, java.io.OutputStream out)
      Just copies all bytes from in to out.
      static void copyContents​(java.io.Reader in, java.io.Writer out)
      Just copies all characters from in to out.
      static java.lang.String getThrowingMethod​(java.lang.Throwable t)
      Returns the class and method name (+a line number) in which the Throwable was thrown.
      static java.io.File newTmpFile​(java.lang.String content)
      Creates a new temporary file using the default encoding of ISO-8859-1 (Latin1).
      static java.io.File newTmpFile​(java.lang.String content, java.nio.charset.Charset encoding)
      Makes a new temporary file and writes content into it.
      static java.lang.String readContents​(java.io.InputStream input, java.lang.String encoding)
      Reads in file contents.
      static java.lang.String readContents​(java.io.Reader in)
      Returns the full contents of the Reader as a String.
      static java.lang.String runSimpleCommand​(java.lang.String command, java.lang.String directory)
      Runs a simple command in given directory.
      • Methods inherited from class java.lang.Object

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

      • newTmpFile

        public static java.io.File newTmpFile​(java.lang.String content,
                                              java.nio.charset.Charset encoding)
                                       throws java.io.IOException
        Makes a new temporary file and writes content into it. The temporary file is created using File.createTempFile(), and the usual semantics apply. The files are not deleted automatically in exit.
        Parameters:
        content - Initial content of the temporary file.
        encoding - Encoding to use.
        Returns:
        The handle to the new temporary file
        Throws:
        java.io.IOException - If the content creation failed.
        See Also:
        File.createTempFile(String,String,File)
      • newTmpFile

        public static java.io.File newTmpFile​(java.lang.String content)
                                       throws java.io.IOException
        Creates a new temporary file using the default encoding of ISO-8859-1 (Latin1).
        Parameters:
        content - The content to put into the file.
        Returns:
        A handle to the newly created file.
        Throws:
        java.io.IOException - If writing was unsuccessful.
        See Also:
        newTmpFile( String, Charset ), File.createTempFile(String,String,File)
      • runSimpleCommand

        public static java.lang.String runSimpleCommand​(java.lang.String command,
                                                        java.lang.String directory)
                                                 throws java.io.IOException,
                                                        java.lang.InterruptedException
        Runs a simple command in given directory. The environment is inherited from the parent process (e.g. the one in which this Java VM runs).
        Parameters:
        command - The command to run
        directory - The working directory to run the command in
        Returns:
        Standard output from the command.
        Throws:
        java.io.IOException - If the command failed
        java.lang.InterruptedException - If the command was halted
      • copyContents

        public static void copyContents​(java.io.Reader in,
                                        java.io.Writer out)
                                 throws java.io.IOException
        Just copies all characters from in to out. The copying is performed using a buffer of bytes.
        Parameters:
        in - The reader to copy from
        out - The reader to copy to
        Throws:
        java.io.IOException - If reading or writing failed.
        Since:
        1.5.8
      • copyContents

        public static void copyContents​(java.io.InputStream in,
                                        java.io.OutputStream out)
                                 throws java.io.IOException
        Just copies all bytes from in to out. The copying is performed using a buffer of bytes.
        Parameters:
        in - The inputstream to copy from
        out - The outputstream to copy to
        Throws:
        java.io.IOException - In case reading or writing fails.
        Since:
        1.9.31
      • readContents

        public static java.lang.String readContents​(java.io.InputStream input,
                                                    java.lang.String encoding)
                                             throws java.io.IOException
        Reads in file contents.

        This method is smart and falls back to ISO-8859-1 if the input stream does not seem to be in the specified encoding.

        Parameters:
        input - The InputStream to read from.
        encoding - The encoding to assume at first.
        Returns:
        A String, interpreted in the "encoding", or, if it fails, in Latin1.
        Throws:
        java.io.IOException - If the stream cannot be read or the stream cannot be decoded (even) in Latin1
      • readContents

        public static java.lang.String readContents​(java.io.Reader in)
                                             throws java.io.IOException
        Returns the full contents of the Reader as a String.
        Parameters:
        in - The reader from which the contents shall be read.
        Returns:
        String read from the Reader
        Throws:
        java.io.IOException - If reading fails.
        Since:
        1.5.8
      • getThrowingMethod

        public static java.lang.String getThrowingMethod​(java.lang.Throwable t)
        Returns the class and method name (+a line number) in which the Throwable was thrown.
        Parameters:
        t - A Throwable to analyze.
        Returns:
        A human-readable string stating the class and method. Do not rely the format to be anything fixed.