Monday, April 18, 2011

Java Fille I/0

import java.io.*;

public class FileReader {
public static void main (String[] args) {
System.out.println ("Program to demonstrate reading from a file");
BufferedReader br = null;
String fileName = args[0];
// If an error occurs, go to the "catch" block
try {
// FileInputStream fis = new FileInputStream (fileName);
FileReader fis = new FileReader (fileName);
br = new BufferedReader (fis);
// continue to read lines while there are still some left to read
while ( br.ready() ) {
System.out.println (br.readLine());
}
// Close the input stream
br.close();
} catch (Exception e) {
// Handle any error in opening the file
System.err.println("File input error");
}
} // End of main method
} // End of the class FileReader

No comments:

Post a Comment