Class ManFileReadWriteExt


  • public class ManFileReadWriteExt
    extends Object
    Adapted from kotlin.io.FileReadWrite
    • Constructor Detail

      • ManFileReadWriteExt

        public ManFileReadWriteExt()
    • Method Detail

      • reader

        public static InputStreamReader reader​(File thiz)
        Returns a new [FileReader] for reading the content of this file.
      • 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.
      • writer

        public static OutputStreamWriter writer​(File thiz)
        Returns a new [FileWriter] for writing the content of this file.
      • 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.
      • printWriter

        public static PrintWriter printWriter​(File thiz)
        Returns a new [PrintWriter] for writing the content of this file.
      • 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.
      • writeText

        public static void writeText​(File thiz,
                                     String text,
                                     Charset charset)
      • 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.
      • appendText

        public static void appendText​(File thiz,
                                      String text,
                                      Charset charset)
      • 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].