Class FileUtil

java.lang.Object
org.apache.wiki.util.FileUtil

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

    • newTmpFile

      public static File newTmpFile​(String content, Charset encoding) throws 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:
      IOException - If the content creation failed.
      See Also:
      File.createTempFile(String,String,File)
    • newTmpFile

      public static File newTmpFile​(String content) throws 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:
      IOException - If writing was unsuccessful.
      See Also:
      newTmpFile( String, Charset ), File.createTempFile(String,String,File)
    • runSimpleCommand

      public static String runSimpleCommand​(String command, String directory) throws IOException, 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:
      IOException - If the command failed
      InterruptedException - If the command was halted
    • copyContents

      public static void copyContents​(Reader in, Writer out) throws 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:
      IOException - If reading or writing failed.
      Since:
      1.5.8
    • copyContents

      public static void copyContents​(InputStream in, OutputStream out) throws 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:
      IOException - In case reading or writing fails.
      Since:
      1.9.31
    • readContents

      public static String readContents​(InputStream input, String encoding) throws 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:
      IOException - If the stream cannot be read or the stream cannot be decoded (even) in Latin1
    • readContents

      public static String readContents​(Reader in) throws 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:
      IOException - If reading fails.
      Since:
      1.5.8
    • getThrowingMethod

      public static String getThrowingMethod​(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.