Class SimpleFileIO

java.lang.Object
com.helger.io.file.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 Summary

    Modifier and Type
    Method
    Description
    static byte @Nullable []
    getAllFileBytes(@Nullable File aFile)
    Get the content of the file as a byte array.
    static byte @Nullable []
    getAllFileBytes(@Nullable Path aPath)
    Get the content of the file as a byte array.
    static @Nullable com.helger.collection.commons.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.
    static @Nullable String
    getFileAsString(@Nullable File aFile, @NonNull Charset aCharset)
    Get the content of the passed file as a string using the system line separator.
    static List<String>
    readAllLines(@NonNull Path aPath, @NonNull Charset aCharset)
    Read all lines from a file.
    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.
    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.
    static @NonNull com.helger.base.state.ESuccess
    writeFile(@NonNull File aFile, byte @NonNull [] aContent)
    Write the passed byte array to the specified file.
    static @NonNull com.helger.base.state.ESuccess
    writeFile(@NonNull File aFile, byte @NonNull [] aContent, int nOffset, int nLength)
    Write a portion of the passed byte array to the specified file.
    static @NonNull com.helger.base.state.ESuccess
    writeFile(@NonNull File aFile, @NonNull String sContent, @NonNull Charset aCharset)
    Write the passed string to the specified file using the given charset.

    Methods inherited from class java.lang.Object

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

    • getAllFileBytes

      public static byte @Nullable [] 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.
    • getAllFileBytes

      public static byte @Nullable [] getAllFileBytes(@Nullable Path aPath)
      Get the content of the file as a byte array.
      Parameters:
      aPath - The path to read. May be null.
      Returns:
      null if the passed path is null.
      Throws:
      UncheckedIOException - if an I/O error occurs reading from the path
    • 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
    • getFileAsString

      public static @Nullable 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

      public static @Nullable com.helger.collection.commons.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.
    • writeFile

      public static @NonNull com.helger.base.state.ESuccess writeFile(@NonNull File aFile, byte @NonNull [] aContent)
      Write the passed byte array to the specified file.
      Parameters:
      aFile - The file to write to. May not be null.
      aContent - The content to write. May not be null.
      Returns:
      ESuccess.SUCCESS if the file was written successfully, ESuccess.FAILURE otherwise.
    • writeFile

      public static @NonNull com.helger.base.state.ESuccess writeFile(@NonNull File aFile, byte @NonNull [] aContent, @Nonnegative int nOffset, @Nonnegative int nLength)
      Write a portion of the passed byte array to the specified file.
      Parameters:
      aFile - The file to write to. May not be null.
      aContent - The content to write. May not be null.
      nOffset - The offset in the byte array to start writing from. Must be ≥ 0.
      nLength - The number of bytes to write. Must be ≥ 0.
      Returns:
      ESuccess.SUCCESS if the file was written successfully, ESuccess.FAILURE otherwise.
    • writeFile

      public static @NonNull com.helger.base.state.ESuccess writeFile(@NonNull File aFile, @NonNull String sContent, @NonNull Charset aCharset)
      Write the passed string to the specified file using the given charset.
      Parameters:
      aFile - The file to write to. May not be null.
      sContent - The string content to write. May not be null.
      aCharset - The character set to use. May not be null.
      Returns:
      ESuccess.SUCCESS if the file was written successfully, ESuccess.FAILURE otherwise.