Package com.helger.commons.io.file
Class SimpleFileIO
java.lang.Object
com.helger.commons.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[]getAllFileBytes(File aFile) Get the content of the file as a byte array.static byte[]getAllFileBytes(Path aPath) static ICommonsList<String> getAllFileLines(File aFile, Charset aCharset) Get the content of the passed file as a list of lines, whereas each line does not contain a separator.static StringgetFileAsString(File aFile, Charset aCharset) Get the content of the passed file as a string using the system line separator.readAllLines(Path aPath, Charset aCharset) Read all lines from a file.static voidreadFileLines(File aFile, Charset aCharset, 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 voidreadFileLines(File aFile, Charset aCharset, List<String> aTargetList) Get the content of the passed file as a list of lines, whereas each line does not contain a separator.static ESuccessstatic ESuccessstatic ESuccess
-
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
-
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 readSecurityException- In the case of the default provider, and a security manager is installed, thecheckReadmethod is invoked to check read access to the file.
-
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
@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 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
-
writeFile
@Nonnull public static ESuccess writeFile(@Nonnull File aFile, @Nonnull byte[] aContent, @Nonnegative int nOffset, @Nonnegative int nLength) -
writeFile
-