This example shows the followings.
  • how to use FileReader for character stream and
  • do---while control structure to read the contents
  • follow this link to read the content using while loop and using
    • FileReader and BufferRead classes
The contents of the file to read....

package ch4_package;
import java.io.*;
import java.util.Scanner;
public class Test_This {
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String file_name ="";
Process pp = new Process();
try{
//System.out.println("---While Loop---");
System.out.print("Please Enter a File name: ");
file_name = br.readLine();
//System.out.print("Now the text to be added : ");
//str_text = br.readLine();
pp.read_file(file_name);
}//try
catch(NumberFormatException e)
{ System.out.println("data was blank");}
}
}
class Process {
public void read_file(String str)throws IOException
{
FileReader s = null;
int n1;
try {
s = new FileReader(str);
do {
n1 = s.read();
System.out.print((char)n1);
}while (n1 !=-1);
}catch (IOException e) { System.out.println("could not read"); }
finally {
if (s != null) {
s.close();
}

}
}
}