title: Java: Printing program output to a physical printer
body: <p>I am a relatively new programmer so this may be a really simple question to answer but its got me a bit stumped.. </p>
  
  <p>I'm trying to print my Java GUI's final output to a printer. Now, in my GUI, I have it so that when you hit print, a pop-up comes up with the list of available printers, and based on the one you select, it should print to that printer.</p>
  
  <p>However it is not. I got most of my code by scouring the internet for solutions to this problem and found some promising code. However, it printed off of a File. So all I simply do in my method is write my output to a file first so that I can use the same methodology. </p>
  
  <p>A couple things before the method: </p>
  
  <ol>
  <li><p>There are no errors or exceptions thrown.</p></li>
  <li><p>The File I attempt to create every time always exists, and with the correct text.</p></li>
  <li><p>The printer I am printing to IS receiving the print job, it even thinks it has completed it.</p></li>
  </ol>
  
  <p>If I had to guess, I would think I am perhaps writing the output to File in way that the Printer will not except but isn't telling me. Anyways, there is quite a bit in this code that I don't really have a firm understanding of so please let me know what you can find.</p>
  
  <p>Here is my code: </p>
  
  <pre><code>private void printToPrinter()
      {
  
          File output = new File("PrintFile.txt");
          output.setWritable(true);
          //Will become the user-selected printer.
          Object selection = null;
          try 
          {
              BufferedWriter out = new BufferedWriter(new FileWriter(output));
              out.write(calculationTextArea.getText() + "\n" + specificTextArea.getText());
              out.close();
  
          }
          catch (java.io.IOException e)
          {
              System.out.println("Unable to write Output to disk, error occured in saveToFile() Method.");
          }
          FileInputStream textStream = null;
          try 
          {
              textStream = new FileInputStream("PrintFile.txt");
          }
          catch (java.io.FileNotFoundException e)
          {
              System.out.println("Error trying to find the print file created in the printToPrinter() method");
          }
  
          DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
          Doc mydoc = new SimpleDoc(textStream, flavor, null);
  
          //Look up available printers.
          PrintService[] printers = PrintServiceLookup.lookupPrintServices(flavor, null);
  
          if (printers.length == 0)
          {
              // No printers found. Inform user.
              jOptionPane2.showMessageDialog(this, "No printers could be found on your system!", "Error!", JOptionPane.ERROR_MESSAGE);
          }
          else
          {
              selection = jOptionPane2.showInputDialog(this, "Please select the desired printer:", "Print", 
                                                          JOptionPane.INFORMATION_MESSAGE, null, printers,
                                                          PrintServiceLookup.lookupDefaultPrintService()); 
              if (selection instanceof PrintService)
              {
                  PrintService chosenPrinter = (PrintService) selection;
                  DocPrintJob printJob = chosenPrinter.createPrintJob();
                  try 
                  {
                      printJob.print(mydoc, null);
                  }
                  catch (javax.print.PrintException e) 
                  {
                      jOptionPane2.showMessageDialog(this, "Unknown error occured while attempting to print.", "Error!", JOptionPane.ERROR_MESSAGE);
                  }
              }
          }
  }
  </code></pre>