title: Printing text file with Java
body: <p>I am trying to print a text file using the given code. Everything goes fine: the job goes in the printing queue of the printer. However after some time, it disappears and nothing is printed. Also, there isn't an exception or error in the code. Everything goes fine, except it is not printing. I am not using a print API.</p>
  
  <pre><code>public class PipedPrint implements Runnable {
      PipedOutputStream pos = new PipedOutputStream();
      PipedInputStream pis = new PipedInputStream();
      byte[] abPrinterBytes = null;
  
      private void mainProcess() throws IOException, PrintException, InterruptedException {
          abPrinterBytes = getBytesFromFile();
          pis.connect(pos);
          Thread randWriter = new Thread(this);
          randWriter.start();
          PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
          DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
          PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
          PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
          PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
          if (defaultService != null) {
              DocPrintJob job = defaultService.createPrintJob();
              DocAttributeSet das = new HashDocAttributeSet();
              Doc doc = new SimpleDoc(pis, flavor, das);
              System.out.println("Main::Fired Print");
              job.print(doc, pras);
              System.out.println("Main::Done Print");
          }
          randWriter.join();
          System.out.println("Main::Join Over");
      }
  
      private byte[] getBytesFromFile() throws IOException {
          File fFile = new File("C:\\Users\\Wijdan\\Documents\\NetBeansProjects\\demo_print\\src\\demo_print\\pass.txt"); //File to print
          byte[] abFileBytes = new byte[(int) fFile.length()];
          BufferedInputStream in = new BufferedInputStream(new FileInputStream(fFile));
          for (int i = 0; i &lt; abFileBytes.length; i += in .read(abFileBytes, i, (abFileBytes.length - i)));
          return abFileBytes;
      }
  
      public void run() {
          try {
              Thread.sleep(5000);
              System.out.println("RandWriter started Writing");
              for (int i = 0; i &lt; abPrinterBytes.length; i += 400) {
                  pos.write(abPrinterBytes, i, ((i + 400) &lt; abPrinterBytes.length) ? 400 : (abPrinterBytes.length - i));
                  System.out.println("Thread:: Wrote bytes. Sleeping....");
                  Thread.sleep(500);
              }
              pos.close();
              System.out.println("Thread::Closed pos. Exitting Thread...");
          } catch (Exception ie) {
              ie.printStackTrace();
          }
      }
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
          PipedPrint t1 = new PipedPrint();
          try {
              t1.mainProcess();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>