Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class ShowFile2 {
- public static void main(String[] args) {
- int i;
- FileInputStream fin = null; // Here, fin is initialized to null
- // First confirm that a file has been specified.
- if(args.length != 1) {
- System.out.println("Usage: ShowFile filename");
- return;
- }
- // The following code opens a file, reads characters until EOF
- // is encountered, and then closes the file via a finally block.
- try {
- fin = new FileInputStream(args[0]);
- do {
- i = fin.read();
- if(i != -1) System.out.print((char) i);
- } while(i != -1);
- } catch(FileNotFoundException exc) {
- System.out.println("File Not Found");
- } catch(IOException exc) {
- System.out.println("An I/O Error Ocurred");
- } finally {
- // Close file in all cases
- try {
- if(fin != null) fin.close(); // Close the file only if fin is not null
- } catch(IOException exc) {
- System.out.println("Error Closing File");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement