5 Ways to Read a File in Java – BufferedReader, FileInputStream, Files, Scanner, RandomAccessFile
In my last post, I was going through with different options we have for SimpleDateFormat. Today I am providing a program for reading file in Java.
I am using BufferedReader for reading the file because it buffers the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.
Here I am providing three functions.
1. Read complete file as String
2. Read file line by line and return list of String
3. Count the occurrence of a String in the given file.