Class FileIOUtils


  • public final class FileIOUtils
    extends java.lang.Object
    Simple File utils
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  FileIOUtils.TransformingComparator
      Decorates the given comparator and applies the function before delegating to the decorated comparator.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.Comparator<java.lang.String> lexComparator  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void append​(java.util.List<java.io.File> files, java.io.File appendTo, boolean delete)
      Appends the contents of the list of files to the given file and deletes the files if the delete flag is enabled.
      static java.io.File copy​(java.io.InputStream stream)
      Copies an input stream to a file.
      static void copyInputStreamToFile​(java.io.InputStream source, java.io.File destination)
      Copy the input stream to the given file.
      static java.util.Comparator<java.lang.String> lineBreakAwareComparator​(java.util.Comparator<java.lang.String> delegate)
      Composing iterator which unescapes for line breaks and delegates to the given comparator.
      static void merge​(java.util.List<java.io.File> files, java.io.File output)
      Merges a list of files after sorting with the lexComparator.
      static void merge​(java.util.List<java.io.File> files, java.io.File output, java.util.Comparator<java.lang.String> comparator)
      Merges a list of files after sorting with the given comparator.
      static java.util.Set<java.lang.String> readStringsAsSet​(java.io.InputStream stream, boolean unescape)
      Reads strings from the given stream into a set and optionally unescaping for line breaks.
      static void sort​(java.io.File file)
      Sorts the given file externally using the lexComparator and removes duplicates.
      static void sort​(java.io.File file, java.util.Comparator<java.lang.String> comparator)
      Sorts the given file externally with the given comparator and removes duplicates.
      static void writeAsLine​(java.io.BufferedWriter writer, java.lang.String str, boolean escape)
      Writes a string as a new line into the given buffered writer and optionally escapes the line for line breaks.
      static int writeStrings​(java.util.Iterator<java.lang.String> iterator, java.io.File f, boolean escape)
      Writes string from the given iterator to the given file and optionally escape the written strings for line breaks.
      static int writeStrings​(java.util.Iterator<java.lang.String> iterator, java.io.File f, boolean escape, @NotNull java.util.function.Function<java.lang.String,​java.lang.String> transformer, @Nullable Logger logger, @Nullable java.lang.String message)
      Writes string from the given iterator to the given file and optionally escape the written strings for line breaks.
      static int writeStrings​(java.util.Iterator<java.lang.String> iterator, java.io.File f, boolean escape, @Nullable Logger logger, @Nullable java.lang.String message)
      Writes string from the given iterator to the given file and optionally escape the written strings for line breaks.
      • Methods inherited from class java.lang.Object

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

      • lexComparator

        public static final java.util.Comparator<java.lang.String> lexComparator
    • Method Detail

      • sort

        public static void sort​(java.io.File file)
                         throws java.io.IOException
        Sorts the given file externally using the lexComparator and removes duplicates.
        Parameters:
        file - file whose contents needs to be sorted
        Throws:
        java.io.IOException
      • sort

        public static void sort​(java.io.File file,
                                java.util.Comparator<java.lang.String> comparator)
                         throws java.io.IOException
        Sorts the given file externally with the given comparator and removes duplicates.
        Parameters:
        file - file whose contents needs to be sorted
        comparator - to compare
        Throws:
        java.io.IOException
      • merge

        public static void merge​(java.util.List<java.io.File> files,
                                 java.io.File output)
                          throws java.io.IOException
        Merges a list of files after sorting with the lexComparator.
        Parameters:
        files - files to merge
        output - merge output file
        Throws:
        java.io.IOException
      • merge

        public static void merge​(java.util.List<java.io.File> files,
                                 java.io.File output,
                                 java.util.Comparator<java.lang.String> comparator)
                          throws java.io.IOException
        Merges a list of files after sorting with the given comparator.
        Parameters:
        files - files to merge
        output - merge output file
        Throws:
        java.io.IOException
      • copy

        public static java.io.File copy​(java.io.InputStream stream)
                                 throws java.io.IOException
        Copies an input stream to a file.
        Parameters:
        stream - steam to copy
        Returns:
        Throws:
        java.io.IOException
      • append

        public static void append​(java.util.List<java.io.File> files,
                                  java.io.File appendTo,
                                  boolean delete)
                           throws java.io.IOException
        Appends the contents of the list of files to the given file and deletes the files if the delete flag is enabled. If there is a scope for lines in the files containing line break characters it should be ensured that the files are written with writeAsLine(BufferedWriter, String, boolean) with true to escape line break characters.
        Parameters:
        files -
        appendTo -
        Throws:
        java.io.IOException
      • writeAsLine

        public static void writeAsLine​(java.io.BufferedWriter writer,
                                       java.lang.String str,
                                       boolean escape)
                                throws java.io.IOException
        Writes a string as a new line into the given buffered writer and optionally escapes the line for line breaks.
        Parameters:
        writer - to write the string
        str - the string to write
        escape - whether to escape string for line breaks
        Throws:
        java.io.IOException
      • writeStrings

        public static int writeStrings​(java.util.Iterator<java.lang.String> iterator,
                                       java.io.File f,
                                       boolean escape)
                                throws java.io.IOException
        Writes string from the given iterator to the given file and optionally escape the written strings for line breaks.
        Parameters:
        iterator - the source of the strings
        f - file to write to
        escape - whether to escape for line breaks
        Returns:
        count
        Throws:
        java.io.IOException
      • writeStrings

        public static int writeStrings​(java.util.Iterator<java.lang.String> iterator,
                                       java.io.File f,
                                       boolean escape,
                                       @Nullable
                                       @Nullable Logger logger,
                                       @Nullable
                                       @Nullable java.lang.String message)
                                throws java.io.IOException
        Writes string from the given iterator to the given file and optionally escape the written strings for line breaks.
        Parameters:
        iterator - the source of the strings
        f - file to write to
        escape - escape whether to escape for line breaks
        logger - logger to log progress
        message - message to log
        Returns:
        Throws:
        java.io.IOException
      • writeStrings

        public static int writeStrings​(java.util.Iterator<java.lang.String> iterator,
                                       java.io.File f,
                                       boolean escape,
                                       @NotNull
                                       @NotNull java.util.function.Function<java.lang.String,​java.lang.String> transformer,
                                       @Nullable
                                       @Nullable Logger logger,
                                       @Nullable
                                       @Nullable java.lang.String message)
                                throws java.io.IOException
        Writes string from the given iterator to the given file and optionally escape the written strings for line breaks.
        Parameters:
        iterator - the source of the strings
        f - file to write to
        escape - escape whether to escape for line breaks
        transformer - any transformation on the input
        logger - logger to log progress
        message - message to log
        Returns:
        Throws:
        java.io.IOException
      • readStringsAsSet

        public static java.util.Set<java.lang.String> readStringsAsSet​(java.io.InputStream stream,
                                                                       boolean unescape)
                                                                throws java.io.IOException
        Reads strings from the given stream into a set and optionally unescaping for line breaks.
        Parameters:
        stream - the source of the strings
        unescape - whether to unescape for line breaks
        Returns:
        set
        Throws:
        java.io.IOException
      • lineBreakAwareComparator

        public static java.util.Comparator<java.lang.String> lineBreakAwareComparator​(java.util.Comparator<java.lang.String> delegate)
        Composing iterator which unescapes for line breaks and delegates to the given comparator. When using this it should be ensured that the data source has been correspondingly escaped.
        Parameters:
        delegate - the actual comparison iterator
        Returns:
        comparator aware of line breaks
      • copyInputStreamToFile

        public static void copyInputStreamToFile​(java.io.InputStream source,
                                                 java.io.File destination)
                                          throws java.io.IOException
        Copy the input stream to the given file. Delete the file in case of exception.
        Parameters:
        source - the input stream source
        destination - the file to write to
        Throws:
        java.io.IOException