jQuery in Action, 3rd edition
The moose likes Other JSE/JEE APIs and the fly likes Printing Error while printing txt file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Printing Error while printing txt file" Watch "Printing Error while printing txt file" New topic

Printing Error while printing txt file

Rodricks george
Ranch Hand

Joined: Sep 10, 2006
Posts: 97
Hi when i run the below code i am getting Exceptions... like flavour doesnt supp... pls help

import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;

import java.awt.GraphicsConfiguration;
//import java.awt.print.PrinterJob;

public class BasicPrint {
public static void main(String[] args) {
try {
// Open the image file
//InputStream is = new BufferedInputStream(
//new FileInputStream("E:\\websites.txt"));
FileInputStream is = new FileInputStream("E:\\websites.txt");
// Find the default service
//DocFlavor flavor = DocFlavor.READER.TEXT_HTML;
//DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_16BE;
//DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST;
// PrintService service =
// PrintServiceLookup.lookupDefaultPrintService();
PrintService dservice = PrintServiceLookup.lookupDefaultPrintService();

PrintService[] services = PrintServiceLookup.lookupPrintServices(
flavor, null);

if (services == null || services.length < 1)
services = PrintServiceLookup.lookupPrintServices(null, null);

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));
// aset.add(MediaTray.MAIN);

PrintService service = ServiceUI.printDialog(
(GraphicsConfiguration) null, 60, 60, services,
(PrintService) dservice, (DocFlavor) flavor, aset);

if (service != null) {

// Create the print job
final DocPrintJob job = service.createPrintJob();

Doc doc = new SimpleDoc(is, flavor, null);
try {

// Print it

job.print(doc, (PrintRequestAttributeSet) aset);

} catch (PrintException e) {

// Monitor print job events; for the implementation of
// PrintJobWatcher,

//PrintJobWatcher pjDone = new PrintJobWatcher(job);

// Wait for the print job to be done

// It is now safe to close the input stream
}catch (IOException e) {


Exception in thread "main" java.lang.IllegalArgumentException: text/plain; charset="cp1252"; class="java.io.InputStream" is an unsupported flavor
at sun.print.Win32PrintService.getSupportedAttributeValues(Unknown Source)
at sun.print.ServiceDialog$MediaPanel.updateInfo(Unknown Source)
at sun.print.ServiceDialog$PageSetupPanel.updateInfo(Unknown Source)
at sun.print.ServiceDialog.updatePanels(Unknown Source)
at sun.print.ServiceDialog.initPrintDialog(Unknown Source)
at sun.print.ServiceDialog.<init>(Unknown Source)
at javax.print.ServiceUI.printDialog(Unknown Source)
at BasicPrint.main(BasicPrint.java:40)
Joe Ess

Joined: Oct 29, 2001
Posts: 9188

The way I understand Java printing, the DocFlavor isn't used to tell Java what the document is, it is used by Java to tell the printer what the document is. If your printer doesn't support directly printing that kind of document, you receive "unsupported flavor" exceptions.
You can discover the DocFlavors a particular printer can handle with PrintService.getSupportedDocFlavors().
This is probably the same problem you are having with printing PDFs.
I've had the best luck with the AUTOSENSE DocFlavors.

[How To Ask Questions On JavaRanch]
Rodricks george
Ranch Hand

Joined: Sep 10, 2006
Posts: 97
Thank you sir
I agree. Here's the link: http://aspose.com/file-tools
subject: Printing Error while printing txt file
It's not a secret anymore!