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.

Results 1 to 10 of 10

Thread: Print to file with Java

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Print to file with Java

    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


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Print to file with Java

    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.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Print to file with Java

    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.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Print to file with Java

    Please post your code correctly per the link provided by dicdic in post #2.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Print to file with Java

    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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print to file with Java

    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.

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Print to file with Java

     
    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) {
    }


    }

    }

  8. #8
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Print to file with Java

    });

    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)

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print to file with Java

    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.

  10. #10
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Print to file with Java

    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)

Similar Threads

  1. Replies: 6
    Last Post: March 16th, 2014, 08:03 PM
  2. how to get the print preview for pdf file in java
    By sivanand in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 20th, 2013, 12:55 PM
  3. how to print matrix into file ?
    By viki1719 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 19th, 2013, 06:36 AM
  4. How do I print out the whole of my text file?
    By tangara in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 24th, 2013, 11:15 AM
  5. Print to txt file
    By anmason in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 8th, 2013, 09:19 AM