Different ways to Read File (Part 4)
Pre-requisites: Read Files Using Scanner , Exception Handling
Reading a File
We learned to read a file using scanner class , now we will see other ways to read a text file in Java.
1. Using FileReader
FileReader is the most convenient class for reading files. The FileReader returns -1 after executing its code. -1 indicates that the contents have been read.
import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class
MyClass
{ public static void main(String[] args) { File
file
=new File("
C:\\Users\\Desktop\\example.txt
"); try { FileReader
fr
=new FileReader(
file
); int
i
=0; while((
i
=
fr
.read())!= -1) { System.out.print((char)
i
);
//will read i in form of characters
}
fr
.close(); } catch (FileNotFoundException e) { System.out.println("
File Not Found:
"+
file
.toString()); } catch (IOException e) { System.out.println("
Unable to close FileReader.
"); } } }
Output:-
Hello I am a Text File.
This is my 2nd line.
This is my 3rd line.
Thank You.
The condition written inside while block is the general condition written to read lines from file. The condition states that here, int i reads value from FileReader fr till is not equal to -1 i.e null.
2. Using BufferedReader
BufferedReader reads text from character-input stream . It is used to read bytes, arrays, text,etc.
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class
MyClass
{ public static void main(String[] args) { File
file
=new File("
C:\\Users\\Desktop\\example.txt
"); try { FileReader
fr
=new FileReader(
file
);
// BufferedReader is used to wrap FileReader inside it
BufferedReader
br
=new BufferedReader(
fr
); int
i
=0; while((
i
=
br
.read()) != -1) { System.out.print((char)
i
); }
br
.close(); } catch (FileNotFoundException e) { System.out.println("
File Not Found:
"+file.toString()); } catch (IOException e) { System.out.println("
Unable to close BufferedReader.
"); } } }
Output:-
Hello I am a Text File.
This is my 2nd line.
This is my 3rd line.
Thank You.
3. Using InputStream
InputStream reads text from character-input stream . It is used to read any input functionality.
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class
MyClass
{ public static void main(String[] args) { File
file
=new File("
C:\\Users\\Desktop\\example.txt
"); try {
/*As InputStream is an abstract class , we cannot Instantiate it * Therefore we instantiate it with FileInputStream i.e its child functionality */
InputStream
inputStream
=new FileInputStream(
file
); int
i
=0; while((
i
=
inputStream
.read()) != -1) { System.out.print((char)
i
); }
inputStream
.close(); } catch (FileNotFoundException e) { System.out.println("
File Not Found:
"+file.toString()); } catch (IOException e) { System.out.println("
Unable to close InputStream.
"); } } }
Output:-
Hello I am a Text File.
This is my 2nd line.
This is my 3rd line.
Thank You.