Class ManFileReadWriteExt
- java.lang.Object
-
- manifold.io.extensions.java.io.File.ManFileReadWriteExt
-
public class ManFileReadWriteExt extends Object
Adapted from kotlin.io.FileReadWrite
-
-
Constructor Summary
Constructors Constructor Description ManFileReadWriteExt()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidappendBytes(File thiz, byte[] array)Appends an [array] of bytes to the content of this file.static voidappendText(File thiz, String text)Appends [text] to the content of this file using UTF-8 or the specified [charset].static voidappendText(File thiz, String text, Charset charset)static BufferedReaderbufferedReader(File thiz)Returns a new [BufferedReader] for reading the content of this file.static BufferedReaderbufferedReader(File thiz, Charset charset, int bufferSize)static BufferedWriterbufferedWriter(File thiz)Returns a new [BufferedWriter] for writing the content of this file.static BufferedWriterbufferedWriter(File thiz, Charset charset, int bufferSize)static voidforEachBlock(File thiz, int blockSize, BiConsumer<byte[],Integer> action)Reads file by byte blocks and calls [action] for each block read.static voidforEachBlock(File thiz, BiConsumer<byte[],Integer> action)Reads file by byte blocks and calls [action] for each block read.static voidforEachLine(File thiz, Charset charset, Consumer<String> action)Reads this file line by line using the specified [charset] and calls [action] for each line.static FileInputStreaminputStream(File thiz)Constructs a new FileInputStream of this file and returns it as a result.static FileOutputStreamoutputStream(File thiz)Constructs a new FileOutputStream of this file and returns it as a result.static PrintWriterprintWriter(File thiz)Returns a new [PrintWriter] for writing the content of this file.static PrintWriterprintWriter(File thiz, Charset charset)static byte[]readBytes(File thiz)Gets the entire content of this file as a byte array.static InputStreamReaderreader(File thiz)Returns a new [FileReader] for reading the content of this file.static InputStreamReaderreader(File thiz, Charset charset)static List<String>readLines(File thiz, Charset charset)Reads the file content as a list of lines.static StringreadText(File thiz)Gets the entire content of this file as a String using UTF-8 or specified [charset].static StringreadText(File thiz, Charset charset)static <T> TuseLines(File thiz, Charset charset, Function<Iterable<String>,T> block)Calls the [block] callback giving it a sequence of all the lines in this file and closes the reader once the processing is complete.static voidwriteBytes(File thiz, byte[] array)Sets the content of this file as an [array] of bytes.static OutputStreamWriterwriter(File thiz)Returns a new [FileWriter] for writing the content of this file.static OutputStreamWriterwriter(File thiz, Charset charset)static voidwriteText(File thiz, String text)Sets the content of this file as [text] encoded using UTF-8 or specified [charset].static voidwriteText(File thiz, String text, Charset charset)
-
-
-
Method Detail
-
reader
public static InputStreamReader reader(File thiz)
Returns a new [FileReader] for reading the content of this file.
-
reader
public static InputStreamReader reader(File thiz, Charset charset)
-
bufferedReader
public static BufferedReader bufferedReader(File thiz)
Returns a new [BufferedReader] for reading the content of this file.- Parameters:
bufferSize- necessary size of the buffer.
-
bufferedReader
public static BufferedReader bufferedReader(File thiz, Charset charset, int bufferSize)
-
writer
public static OutputStreamWriter writer(File thiz)
Returns a new [FileWriter] for writing the content of this file.
-
writer
public static OutputStreamWriter writer(File thiz, Charset charset)
-
bufferedWriter
public static BufferedWriter bufferedWriter(File thiz)
Returns a new [BufferedWriter] for writing the content of this file.- Parameters:
bufferSize- necessary size of the buffer.
-
bufferedWriter
public static BufferedWriter bufferedWriter(File thiz, Charset charset, int bufferSize)
-
printWriter
public static PrintWriter printWriter(File thiz)
Returns a new [PrintWriter] for writing the content of this file.
-
printWriter
public static PrintWriter printWriter(File thiz, Charset charset)
-
readBytes
public static byte[] readBytes(File thiz)
Gets the entire content of this file as a byte array.This method is not recommended on huge files. It has an internal limitation of 2 GB byte array size.
- Returns:
- the entire content of this file as a byte array.
-
writeBytes
public static void writeBytes(File thiz, byte[] array)
Sets the content of this file as an [array] of bytes. If this file already exists, it becomes overwritten.- Parameters:
array- byte array to write into this file.
-
appendBytes
public static void appendBytes(File thiz, byte[] array)
Appends an [array] of bytes to the content of this file.- Parameters:
array- byte array to append to this file.
-
readText
public static String readText(File thiz)
Gets the entire content of this file as a String using UTF-8 or specified [charset].This method is not recommended on huge files. It has an internal limitation of 2 GB file size.
- Parameters:
charset- character set to use.- Returns:
- the entire content of this file as a String.
-
writeText
public static void writeText(File thiz, String text)
Sets the content of this file as [text] encoded using UTF-8 or specified [charset]. If this file exists, it becomes overwritten.- Parameters:
text- text to write into file.charset- character set to use.
-
appendText
public static void appendText(File thiz, String text)
Appends [text] to the content of this file using UTF-8 or the specified [charset].- Parameters:
text- text to append to file.charset- character set to use.
-
forEachBlock
public static void forEachBlock(File thiz, BiConsumer<byte[],Integer> action)
Reads file by byte blocks and calls [action] for each block read. Block has default size which is implementation-dependent. This functions passes the byte array and amount of bytes in the array to the [action] function.You can use this function for huge files.
- Parameters:
action- function to process file blocks.
-
forEachBlock
public static void forEachBlock(File thiz, int blockSize, BiConsumer<byte[],Integer> action)
Reads file by byte blocks and calls [action] for each block read. This functions passes the byte array and amount of bytes in the array to the [action] function.You can use this function for huge files.
- Parameters:
action- function to process file blocks.blockSize- size of a block, replaced by 512 if it's less, 4096 by default.
-
forEachLine
public static void forEachLine(File thiz, Charset charset, Consumer<String> action)
Reads this file line by line using the specified [charset] and calls [action] for each line. Default charset is UTF-8.You may use this function on huge files.
- Parameters:
charset- character set to use.action- function to process file lines.
-
inputStream
public static FileInputStream inputStream(File thiz)
Constructs a new FileInputStream of this file and returns it as a result.
-
outputStream
public static FileOutputStream outputStream(File thiz)
Constructs a new FileOutputStream of this file and returns it as a result.
-
readLines
public static List<String> readLines(File thiz, Charset charset)
Reads the file content as a list of lines.Do not use this function for huge files.
- Parameters:
charset- character set to use. By default uses UTF-8 charset.- Returns:
- list of file lines.
-
useLines
public static <T> T useLines(File thiz, Charset charset, Function<Iterable<String>,T> block)
Calls the [block] callback giving it a sequence of all the lines in this file and closes the reader once the processing is complete.- Parameters:
charset- character set to use. By default uses UTF-8 charset.- Returns:
- the value returned by [block].
-
-