Package toxi.util

Class FileUtils

java.lang.Object
toxi.util.FileUtils

public class FileUtils extends Object
A collection of file handling utilities.
  • Field Details

  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • createDirectories

      public static boolean createDirectories(File path)
      Attempts to create the full path of directories as specified by the given target path. The path is assumed to be a directory, NOT a file in a directory. For the latter, use createDirectoriesForFile(File).
      Parameters:
      path -
      Returns:
      true, if the operation succeeded
    • createDirectoriesForFile

      public static boolean createDirectoriesForFile(File file)
      Attempts to create the full path of directories as specified by the given target file.
      Parameters:
      file -
      Returns:
      true, if the operation succeeded
    • createInputStream

      public static InputStream createInputStream(File file) throws IOException
      Creates an InputStream for the given file. If the file extension ends with ".gz" the stream is automatically wrapped in a GZIPInputStream as well.
      Parameters:
      file - input file
      Returns:
      input stream
      Throws:
      IOException
    • createOutputStream

      public static OutputStream createOutputStream(File file) throws IOException
      Creates an OutputStream for the given file. If the file extension ends with ".gz" the stream is automatically wrapped in a GZIPOutputStream as well. Also attempts to create any intermediate directories for the file using createDirectoriesForFile(File).
      Parameters:
      file - output file
      Returns:
      output stream
      Throws:
      IOException
    • createReader

      public static BufferedReader createReader(File file) throws IOException
      Creates a BufferedReader for the given file using UTF-8 encoding.
      Parameters:
      file -
      Returns:
      reader instance
      Throws:
      IOException
    • createReader

      public static BufferedReader createReader(InputStream input)
      Creates a BufferedReader for the given InputStream using UTF-8 encoding.
      Parameters:
      input - stream
      Returns:
      reader instance
      Throws:
      IOException
    • createReader

      public static BufferedReader createReader(InputStream input, String encoding)
      Creates a BufferedReader for the given InputStream and using the specified encoding.
      Parameters:
      input - stream
      encoding - text encoding to use
      Returns:
      reader instance
      Throws:
      IOException
    • createWriter

      public static BufferedWriter createWriter(File file) throws IOException
      Creates a BufferedWriter for the given file using UTF-8 encoding.
      Parameters:
      file -
      Returns:
      writer instance
      Throws:
      IOException
    • createWriter

      public static BufferedWriter createWriter(OutputStream out)
      Creates a BufferedWriter for the given OutputStream using UTF-8 encoding.
      Parameters:
      out -
      Returns:
      writer instance
      Throws:
      IOException
    • createWriter

      public static BufferedWriter createWriter(OutputStream out, String encoding)
      Creates a BufferedWriter for the given OutputStream and using the specified encoding.
      Parameters:
      out - stream
      encoding - text encoding to use
      Returns:
      writer instance
      Throws:
      IOException
    • getFileSequenceDescriptorFor

      public static FileSequenceDescriptor getFileSequenceDescriptorFor(String path)

      Analyses the given file path for a file sequence pattern and returns a FileSequenceDescriptor instance for further use to handle this sequence. The file pattern should be in one of these formats:

      • base_path-00001.ext
      • base_path001.ext

      The sequence index should be using leading zeros, but the number of digits will be identified automatically.

      Parameters:
      path - file path of the first file in the sequence
      Returns:
      descriptor, or null, if the path could not be analysed
    • loadBytes

      public static byte[] loadBytes(InputStream stream) throws IOException
      Loads the given InputStream into a byte array buffer.
      Parameters:
      stream -
      Returns:
      byte array
      Throws:
      IOException
    • loadText

      public static String loadText(BufferedReader reader) throws IOException
      Loads the given BufferedReader as text, line by line into a String.
      Parameters:
      reader -
      Returns:
      reader contents as string
      Throws:
      IOException
    • loadText

      public static String loadText(InputStream input) throws IOException
      Loads the given InputStream as text, line by line into a String using UTF-8 encoding.
      Parameters:
      input - stream
      Returns:
      stream contents as string
      Throws:
      IOException
    • loadText

      public static String loadText(InputStream input, String encoding) throws IOException
      Loads the given InputStream as text, line by line into a String using the specified encoding.
      Parameters:
      input - stream
      encoding -
      Returns:
      stream contents as single string
      Throws:
      IOException
    • saveText

      public static void saveText(BufferedWriter writer, String string) throws IOException
      Saves the given text to the specified BufferedWriter instance, then closes the writer afterwards.
      Parameters:
      writer -
      string -
      Throws:
      IOException
    • saveText

      public static void saveText(OutputStream output, String string) throws IOException
      Saves the given text to the specified OutputStream instance, then closes the underlying writer afterwards.
      Parameters:
      output -
      string -
      Throws:
      IOException
    • showDirectoryChooser

      public static String showDirectoryChooser(Frame frame, String title, String path)
      Displays a standard AWT file dialog for choosing a folder (no files!).
      Parameters:
      frame - parent frame
      title - dialog title
      path - base directory (or null)
      Returns:
      path to chosen directory or null, if user has canceled
    • showFileDialog

      public static String showFileDialog(Frame frame, String title, String path, FilenameFilter filter, int mode)
      Displays a standard AWT file dialog for choosing a file for loading or saving.
      Parameters:
      frame - parent frame
      title - dialog title
      path - base directory (or null)
      filter - a FilenameFilter implementation (or null)
      mode - either FileUtils.LOAD or FileUtils.SAVE
      Returns:
      path to chosen file or null, if user has canceled
    • showFileDialog

      public static String showFileDialog(Frame frame, String title, String path, String[] formats, int mode)
      Displays a standard AWT file dialog for choosing a file for loading or saving.
      Parameters:
      frame - parent frame
      title - dialog title
      path - base directory (or null)
      formats - an array of allowed file extensions (or null to allow all)
      mode - either FileUtils.LOAD or FileUtils.SAVE
      Returns:
      path to chosen file or null, if user has canceled