Hi,
In my project i need to use printer concepts to print a file but the output should be printed to another output file instead of printer,
I am totally new to java,please provide any solution for my problem
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi,
In my project i need to use printer concepts to print a file but the output should be printed to another output file instead of printer,
I am totally new to java,please provide any solution for my problem
Welcome! Please read Announcements - What's Wrong With My Code?
You can do internet search to find a way on how to read from and write into a file. Then try to code it for yourself. Once you have the code, but some errors or bugs occurs you can ask here. Or even if you successfully made the program but have doubts if your coding is okay, you can also create a thread here to ask.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.print.CancelablePrintJob;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.SimpleDoc;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
public class Example2 {
public static void main(String[] args) throws Exception {
OutputStream fos = new BufferedOutputStream(new FileOutputStream("/home/Desktop/sample1.txt"));
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDFTEXT_PLAIN_HOST;
InputStream is = new BufferedInputStream(new FileInputStream("/home/Desktop/sample.txt"));
StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
.lookupStreamPrintServiceFactories(flavor, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());
StreamPrintService service = factories[0].getPrintService(fos);
final DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
if (job instanceof CancelablePrintJob) {
CancelablePrintJob cancelJob = (CancelablePrintJob) job;
try {
cancelJob.cancel();
} catch (PrintException e) {
}
}
job.print(doc, null);
pjDone.waitForDone();
is.close();
}
}
class PrintJobWatcher {
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobCompleted(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobFailed(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}
This is my complete program , I am getting error like
sun.print.PrintJobFlavorException: invalid flavor
please help mw whats wrong in my code.
Please post your code correctly per the link provided by dicdic in post #2.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.print.CancelablePrintJob;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.SimpleDoc;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
public class Example2 {
public static void main(String[] args) throws Exception {
OutputStream fos = new BufferedOutputStream(new FileOutputStream("/home/Desktop/sample1.txt"));
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDFTEXT_PLAIN_HOST;
InputStream is = new BufferedInputStream(new FileInputStream("/home/Desktop/sample.txt"));
StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintService Factories(flavor,DocFlavor.BYTE_ARRAY.POSTSCRIPT.g etMimeType());
StreamPrintService service = factories[0].getPrintService(fos);
final DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
if (job instanceof CancelablePrintJob) {
CancelablePrintJob cancelJob = (CancelablePrintJob) job;
try {
cancelJob.cancel();
} catch (PrintException e) {
}
}
job.print(doc, null);
pjDone.waitForDone();
is.close();
}
}
class PrintJobWatcher {
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobCompleted(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobFailed(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}
This is my complete program , I am getting error like
sun.print.PrintJobFlavorException: invalid flavor
please help me whats wrong in my code and what are the necessary changes need to be done.
Thanks In Advance
Please edit your post and wrap your code with code tags:
[code=java]
YOUR CODE GOES HERE
[/code]
to get highlighting and preserve formatting.
Copy the full text of the error message and paste it, not just one line.
If you don't understand my answer, don't ignore it, ask a question.
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import javax.print.CancelablePrintJob; import javax.print.Doc; import javax.print.DocFlavor; import javax.print.DocPrintJob; import javax.print.PrintException; import javax.print.SimpleDoc; import javax.print.StreamPrintService; import javax.print.StreamPrintServiceFactory; import javax.print.event.PrintJobAdapter; import javax.print.event.PrintJobEvent; public class Example2 { public static void main(String[] args) throws Exception { OutputStream fos = new BufferedOutputStream(new FileOutputStream("/home/Desktop/sample1.ps")); System.out.println("fos : " + fos); DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; // DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; System.out.println("flavor : " + flavor); InputStream is = new BufferedInputStream(new FileInputStream("/home/Desktop/java.txt")); System.out.println("is : " + is); StreamPrintServiceFactory[] factories = StreamPrintServiceFactory .lookupStreamPrintServiceFactories(null, DocFlavor.INPUT_STREAM.POSTSCRIPT.getMimeType()); //StreamPrintServiceFactory[] factories = StreamPrintServiceFactory // .lookupStreamPrintServiceFactories(flavor, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType()); if (factories.length == 0) { System.out.println("Unable to print files of type: " ); } System.out.println("factories : " + factories); System.out.println("DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType() : " + DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType()); StreamPrintService service = factories[0].getPrintService(fos); System.out.println("service : " + service); DocPrintJob job = service.createPrintJob(); System.out.println("job : " + job); Doc doc = new SimpleDoc(is, flavor, null); System.out.println("doc: " + doc); PrintJobWatcher pjDone = new PrintJobWatcher(job); System.out.println("pjDone : " + pjDone); if (job instanceof CancelablePrintJob) { CancelablePrintJob cancelJob = (CancelablePrintJob) job; System.out.println("cancelJob : " + cancelJob); try { cancelJob.cancel(); } catch (PrintException e) { } } job.print(doc, null); pjDone.waitForDone(); is.close(); } }
Error
fos : java.io.BufferedOutputStream@6bbc4459
flavor : application/octet-stream; class="java.io.InputStream"
is : java.io.BufferedInputStream@5d888759
factories : [Ljavax.print.StreamPrintServiceFactory;@15e0be38
DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType() : application/postscript
service : PSStreamPrintService: Postscript output
job : sun.print.PSStreamPrintJob@4f037c71
doc: javax.print.SimpleDoc@620b66cc
pjDone : printer.com.PrintJobWatcher@5b3caecd
cancelJob : sun.print.PSStreamPrintJob@4f037c71
Exception in thread "main" sun.print.PrintJobFlavorException: invalid flavor
at sun.print.PSStreamPrintJob.print(PSStreamPrintJob. java:271)
at printer.com.Example2.main(Example2.java:93)
class PrintJobWatcher {
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobCompleted(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobFailed(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}
});
Does that look right to you?
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)
Hard to say if that is ok with the code being unformated.
If you don't understand my answer, don't ignore it, ask a question.
class PrintJobWatcher { boolean done = false; PrintJobWatcher(DocPrintJob job) { job.addPrintJobListener(new PrintJobAdapter() { public void printJobCanceled(PrintJobEvent pje) { synchronized (PrintJobWatcher.this) { done = true; PrintJobWatcher.this.notify(); } } public void printJobCompleted(PrintJobEvent pje) { synchronized (PrintJobWatcher.this) { done = true; PrintJobWatcher.this.notify(); } } public void printJobFailed(PrintJobEvent pje) { synchronized (PrintJobWatcher.this) { done = true; PrintJobWatcher.this.notify(); } } public void printJobNoMoreEvents(PrintJobEvent pje) { synchronized (PrintJobWatcher.this) { done = true; PrintJobWatcher.this.notify(); } } } ); // this really should not be here } // what's going on here? No braces are matching before a new method is called // LOL I give up! public synchronized void waitForDone() { try { while (!done) { wait(); } } catch (InterruptedException e) { } } }
Well I tried to sort it out (format) but other than re-writing it from scratch on Eclipse
it's so hard to read.
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)