Write a Java program to read the contents of file using Character Stream.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
class ReadFromFile {
public static void main(String[] args) throws Exception {
FileReader inFile = new FileReader(args[0]);
BufferedReader br = new BufferedReader(inFile);
String s;
System.out.println("The contents of " + args[0] + "file are....");
/* Read lines from the file and display them on the screen. */
while((s = br.readLine()) != null) {
System.out.println(s);
}
inFile.close();
}
}
C:\>javac ReadFromFile.java
C:\>java ReadFromFile test.txt
The contents of test.txtfile are....
Hi
This is printing from
file 'text.txt'.