joseleonweb

Untitled

Feb 28th, 2021
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class ShowFile3 {
  4.  
  5.     public static void main(String[] args) {
  6.         int i;
  7.        
  8.         // First make sure that a file name has been specified.
  9.         if(args.length != 1) {
  10.             System.out.println("Usage: ShowFile filename");
  11.             return;
  12.         }
  13.        
  14.         // The following code uses try-with-resources to open a file
  15.         // and then automatically close it when the try block is left.
  16.        
  17.         try(FileInputStream fin = new FileInputStream(args[0])) {
  18.            
  19.             do {
  20.                 i = fin.read();
  21.                 if(i != -1) System.out.print((char) i);
  22.             } while(i != -1);
  23.            
  24.         } catch(IOException exc) {
  25.             System.out.println("I/O Error: " + exc);
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment