Class ReadFileExtensions

java.lang.Object
io.github.astrapi69.file.read.ReadFileExtensions

public final class ReadFileExtensions extends Object
The class ReadFileExtensions helps in reading files.
  • Method Details

    • getFilecontentAsByteObjectArray

      public static Byte[] getFilecontentAsByteObjectArray(File tmpFile) throws IOException
      Gets the file content as Byte array from the given file
      Parameters:
      tmpFile - the temporary file
      Returns:
      the file content as Byte array object
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • readFileToBytearray

      public static byte[] readFileToBytearray(File file) throws IOException
      Reads the given file to a byte array
      Parameters:
      file - the file to read
      Returns:
      byte array of the file content or null
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • fromFile

      public static String fromFile(File file) throws IOException
      Reads the given file with UTF-8 encoding and returns the content as String
      Parameters:
      file - the file to read
      Returns:
      String content from the file
      Throws:
      IOException - Signals that an I/O exception has occurred
    • fromFile

      public static String fromFile(File file, Charset encoding) throws IOException
      Reads the given file with the specified encoding and returns the content as String
      Parameters:
      file - the file to read
      encoding - the encoding to use
      Returns:
      String content from the file
      Throws:
      IOException - Signals that an I/O exception has occurred
    • readHeadLine

      public static String readHeadLine(String inputFile) throws FileNotFoundException, IOException
      Reads the first line from the specified file
      Parameters:
      inputFile - the file path
      Returns:
      first line from the file
      Throws:
      FileNotFoundException - is thrown if the given file is not found.
      IOException - Signals that an I/O exception has occurred.
    • readLinesInList

      public static List<String> readLinesInList(File input) throws IOException
      Reads all lines from the specified file into a list
      Parameters:
      input - the file to read
      Returns:
      list of lines from the file
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • readLine

      public static String readLine(File input, int index) throws FileNotFoundException, IOException
      Reads a line at the specified index from the file, or null if the line does not exist
      Parameters:
      input - the file to read
      index - The index of the line
      Returns:
      The line or null if it does not exist
      Throws:
      FileNotFoundException - is thrown if the given file is not found.
      IOException - Signals that an I/O exception has occurred.
    • readLinesInList

      public static List<String> readLinesInList(File input, boolean trim) throws FileNotFoundException, IOException
      Reads every line from the File and puts them to the List.
      Parameters:
      input - The File from where the input comes.
      trim - the flag trim if the lines shell be trimed.
      Returns:
      The List with all lines from the file.
      Throws:
      FileNotFoundException - is thrown if the given file is not found.
      IOException - Signals that an I/O exception has occurred.
    • readLinesInList

      public static List<String> readLinesInList(File input, Charset encoding) throws FileNotFoundException, IOException
      Reads every line from the File and puts them to the List.
      Parameters:
      input - The File from where the input comes.
      encoding - the encoding
      Returns:
      The List with all lines from the file.
      Throws:
      FileNotFoundException - is thrown if the given file is not found.
      IOException - When a io-problem occurs.
    • readLinesInList

      public static List<String> readLinesInList(File input, Charset encoding, boolean trim) throws FileNotFoundException, IOException
      Reads every line from the File and puts them to the List.
      Parameters:
      input - The File from where the input comes.
      encoding - the charset for read
      trim - the flag trim if the lines shell be trimed.
      Returns:
      The List with all lines from the file.
      Throws:
      FileNotFoundException - is thrown if the given file is not found.
      IOException - When a io-problem occurs.
    • readLinesInList

      public static List<String> readLinesInList(InputStream input) throws IOException
      Reads every line from the given InputStream and puts them to the List.
      Parameters:
      input - The InputStream from where the input comes.
      Returns:
      The List with all lines from the file.
      Throws:
      IOException - When a io-problem occurs.
    • readLinesInList

      public static List<String> readLinesInList(InputStream input, boolean trim) throws IOException
      Reads every line from the given InputStream and puts them to the List.
      Parameters:
      input - The InputStream from where the input comes.
      trim - the flag trim if the lines shell be trimed.
      Returns:
      The List with all lines from the file.
      Throws:
      IOException - When a io-problem occurs.
    • readLinesInList

      public static List<String> readLinesInList(InputStream input, Charset encoding, boolean trim) throws IOException
      Reads every line from the given InputStream and puts them to the List.
      Parameters:
      input - The InputStream from where the input comes.
      encoding - the charset for read
      trim - the flag trim if the lines shell be trimed.
      Returns:
      The List with all lines from the file.
      Throws:
      IOException - When a io-problem occurs.
    • readPropertiesFromFile

      public static Properties readPropertiesFromFile(String filename) throws IOException
      Reads properties from a properties file
      Parameters:
      filename - the properties file path
      Returns:
      Properties object or null if error occurs
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • toByteArray

      public static byte[] toByteArray(File tmpFile) throws IOException
      Reads file content into a byte array
      Parameters:
      tmpFile - the file to read
      Returns:
      byte array of file content or null
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • toByteArray

      public static byte[] toByteArray(InputStream input) throws IOException
      Get a byte array from the given InputStream.
      Parameters:
      input - The InputStream.
      Returns:
      the a byte array.
      Throws:
      IOException - Signals that an I/O exception has occurred.
      NullPointerException - if the input is null
    • countAllLines

      public static long countAllLines(File fileToCountTheLines) throws IOException
      Counts all lines in the specified file
      Parameters:
      fileToCountTheLines - the file to count lines from
      Returns:
      line count
      Throws:
      IOException - Signals that an I/O exception has occurred.