import java.io.*;
//////////////////////////////////////////////////////////////////
// Imprimir un fichero de texto
//
public class Imprimir
{
  public static void imprimir(FileInputStream fe) throws IOException
  {
    byte[] buffer = new byte[81];
    int nbytes = 0;

    // Crear un flujo hacia la impresora
    FileWriter flujoS = new FileWriter("LPT1");
  
    do
    {
      // Leer del fichero de texto
      nbytes = fe.read(buffer, 0, 81);
      if (nbytes == -1) break;
      // Crear un objeto String con el texto leído
      String str = new String(buffer, 0, nbytes);
      // Imprimir el texto leído
      flujoS.write(str);
    }
    while (true);

    flujoS.write("\f"); // saltar a la siguiente página
    flujoS.close();     // cerrar el flujo hacia la impresora
  }
  
  public static void main(String[] args)
  {
    FileInputStream fe = null;
    File fichero = null;
    
    try
    {
      if (args.length == 0)
      {
        System.out.println("Sintaxis: java imprimir fichero");
        return;
      }

      fichero = new File(args[0]);

      if (!fichero.exists())
      {
        System.out.println("El fichero no existe");
        return;
      }
      
      // Crear un flujo desde el fichero
      fe = new FileInputStream(fichero);
      // Imprimir el fichero de texto
      imprimir(fe);
    }
    catch(IOException e)
    {
      System.out.println("Error: " + e.toString());
    }
    finally
    {
      try
      {
        // Cerrar el fichero
        if (fe != null) fe.close();
      }
      catch(IOException e)
      {
        System.out.println("Error: " + e.toString());
      }
    }
  }
}
