package pbt.iotest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;



/**
 * Exemple d'utilisation des classes; 
 *  - {@link InputStream}
 *  - {@link FileInputStream}
 * Écriture d'un "cat" limité à un seul fichier en entrée ou 
 * à l'entrée standard. 
 * @author pbt
 */
public class Cat {

	public static void main(String[] args) {
		final String USAGE = "Usage: java Cat [filename]" ;		
		
		if ( args.length > 1 ) {
			System.err.println(USAGE);
			System.exit(1) ; 
		}
		
		// À ce stade, je suis sûr du nombre de paramètres
		InputStream in = System.in ;
		int b ; 
		try {
			if ( args.length == 1) {
				in = new FileInputStream(args[0]) ;
			}
			while ( (b=in.read()) != -1 ) {
				// Attention: "cast vers char"
				System.out.print( (char)b);
			}
			in.close() ; 
		} catch ( FileNotFoundException fnfe ) {
			System.err.println("File not found " + fnfe.getMessage() );
		} catch ( IOException ioe ) {
			System.err.println("IO problem " + ioe.getMessage() );
		}
		
		

	}

}
