Package com.helger.io.file
Class SimpleFileIO
java.lang.Object
com.helger.io.file.SimpleFileIO
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 TypeMethodDescriptionstatic 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 StringgetFileAsString(@Nullable File aFile, @NonNull Charset aCharset) Get the content of the passed file as a string using the system line separator.readAllLines(@NonNull Path aPath, @NonNull Charset aCharset) Read all lines from a file.static voidreadFileLines(@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 voidreadFileLines(@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.ESuccessWrite the passed byte array to the specified file.static @NonNull com.helger.base.state.ESuccessWrite a portion of the passed byte array to the specified file.static @NonNull com.helger.base.state.ESuccessWrite the passed string to the specified file using the given charset.
-
Method Details
-
getAllFileBytes
Get the content of the file as a byte array.- Parameters:
aFile- The file to read. May benull.- Returns:
nullif the passed file isnullor if the passed file does not exist.
-
getAllFileBytes
Get the content of the file as a byte array.- Parameters:
aPath- The path to read. May benull.- Returns:
nullif the passed path isnull.- 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:
\u000Dfollowed 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 fileaCharset- the charset to use for decoding- Returns:
- the lines from the file as a
List; whether theListis 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
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 benull.aCharset- The character set to use. May not benull.- Returns:
nullif 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 benull.aCharset- The character set to use. May not benull.- Returns:
nullif 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 benull.aCharset- The character set to use. May not benull.aTargetList- The target list to be filled. May not benull.
-
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 benull.aCharset- The character set to use. May not benull.aConsumer- The consumer to be invoked for each line. May not benull.
-
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 benull.aContent- The content to write. May not benull.- Returns:
ESuccess.SUCCESSif the file was written successfully,ESuccess.FAILUREotherwise.
-
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 benull.aContent- The content to write. May not benull.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.SUCCESSif the file was written successfully,ESuccess.FAILUREotherwise.
-
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 benull.sContent- The string content to write. May not benull.aCharset- The character set to use. May not benull.- Returns:
ESuccess.SUCCESSif the file was written successfully,ESuccess.FAILUREotherwise.
-