4

I created a text file using java and saved it using this code:

BufferedWriter bfw;
bfw = new BufferedWriter(new FileWriter("D:\\abc.txt"));

Now I want to call the printer from my java code to print the file, how do I do this ?

  • possible duplicate of [Print text File to specific printer in java](http://stackoverflow.com/questions/1097346/print-text-file-to-specific-printer-in-java) – Mailkov Dec 31 '14 at 12:34
  • Maybe this link will be helpful: http://www.rgagnon.com/javadetails/java-print-a-text-file-with-javax.print-api.html – WeSt Dec 31 '14 at 12:34
  • im not that good in java so i need a simplest way of doing this – Hiran Erangana Dec 31 '14 at 12:36

2 Answers2

0

Maybe look into an API that supports printing.

If you are using java 1.7 higher you can use this one. You can find an example inside the documentation.

http://docs.oracle.com/javase/7/docs/api/javax/print/package-summary.html

MajorT
  • 221
  • 1
  • 12
0

A simple way to print to a specific printer which you select in a printdialog:

    JEditorPane text = new JEditorPane("file:///D:/abc.txt");
    text.print(null, null, true, null, null, false);

To print to the default printer without printdialog:

    JEditorPane text = new JEditorPane("file:///D:/abc.txt");
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();
    text.print(null, null, false, service, null, false);
Gren
  • 1,850
  • 1
  • 11
  • 16