import java.io.BufferedReader;
import java.io.FileReader;
/**
* This program reads a text file line by line and print to the console.
*/
public class MyFileReader {
public static void main(String[] args) {
System.out.println("====================================");
System.out.println("Main 1. Check parameter");
System.out.println("====================================");
// First chech nr of args.
if (args.length != 1){
System.err.println ("Usage: java FileReader file_path");
System.exit(2);
}
String path=args[0];
try{
// Open the file
FileReader fr = new FileReader(path);
// Buffer the Reader
BufferedReader br = new BufferedReader(fr);
System.out.println("====================================");
System.out.println("Main 2. Print file contents");
System.out.println("====================================");
String s;
while ((s = br.readLine()) != null) {
System.out.println (s);
}
//Close the file
br.close();
fr.close();
System.out.println("====================================");
System.out.println("Main 3. All done");
System.out.println("====================================");
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}
}
|