Class SimpleFileIO


  • @Immutable
    public final class SimpleFileIO
    extends Object
    All kind of file handling stuff. For other operations, please see FileOperations class or the instance based FileOperationManager class.
    Author:
    Philip Helger
    • Method Detail

      • getAllFileBytes

        @Nullable
        public static byte[] getAllFileBytes​(@Nullable
                                             File aFile)
        Get the content of the file as a byte array.
        Parameters:
        aFile - The file to read. May be null.
        Returns:
        null if the passed file is null or if the passed file does not exist.
      • readAllLines

        public static List<String> readAllLines​(@Nonnull
                                                Path aPath,
                                                @Nonnull
                                                Charset aCharset)
                                         throws IOException
        Read all lines from a file. This method ensures that the file is closed when all bytes have been read or an I/O error, or other runtime exception, is thrown. Bytes from the file are decoded into characters using the specified charset.

        This method recognizes the following as line terminators:

        • \u000D followed by \u000A, CARRIAGE RETURN followed by LINE FEED
        • \u000A, LINE FEED
        • \u000D, CARRIAGE RETURN

        Additional Unicode line terminators may be recognized in future releases.

        Note that this method is intended for simple cases where it is convenient to read all lines in a single operation. It is not intended for reading in large files.

        Parameters:
        aPath - the path to the file
        aCharset - the charset to use for decoding
        Returns:
        the lines from the file as a List; whether the List is modifiable or not is implementation dependent and therefore not specified
        Throws:
        IOException - if an I/O error occurs reading from the file or a malformed or unmappable byte sequence is read
        SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the file.
      • getFileAsString

        @Nullable
        public static String getFileAsString​(@Nullable
                                             File aFile,
                                             @Nonnull
                                             Charset aCharset)
        Get the content of the passed file as a string using the system line separator. Note: the last line does not end with the passed line separator.
        Parameters:
        aFile - The file to read. May be null.
        aCharset - The character set to use. May not be null.
        Returns:
        null if the file does not exist, the content otherwise.
      • getAllFileLines

        @Nullable
        public static ICommonsList<String> getAllFileLines​(@Nullable
                                                           File aFile,
                                                           @Nonnull
                                                           Charset aCharset)
        Get the content of the passed file as a list of lines, whereas each line does not contain a separator.
        Parameters:
        aFile - The file to read. May be null.
        aCharset - The character set to use. May not be null.
        Returns:
        null if the file does not exist, the content otherwise.
      • readFileLines

        public static void readFileLines​(@Nullable
                                         File aFile,
                                         @Nonnull
                                         Charset aCharset,
                                         @Nonnull
                                         List<String> aTargetList)
        Get the content of the passed file as a list of lines, whereas each line does not contain a separator.
        Parameters:
        aFile - The file to read. May be null.
        aCharset - The character set to use. May not be null.
        aTargetList - The target list to be filled. May not be null.
      • readFileLines

        public static void readFileLines​(@Nullable
                                         File aFile,
                                         @Nonnull
                                         Charset aCharset,
                                         @Nonnull
                                         Consumer<? super String> aConsumer)
        Get the content of the passed file as a list of lines, whereas each line does not contain a separator.
        Parameters:
        aFile - The file to read. May be null.
        aCharset - The character set to use. May not be null.
        aConsumer - The consumer to be invoked for each line. May not be null.