Printing to a printer

I am using VJ++ (WFC). How can I print some string and variables to a printer? Moreover, how can I specify to print at a particular printer port?
tracypoonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tracypoonAuthor Commented:
Thanks!
0
vladi21Commented:

     If you simply want to print text, then write it to a file and print the
     file. Or open a filename that corresponds to the printer. On Windows,
     that is "LPT1" and the code looks like:

     try {
         FileOutputStream fos = new FileOutputStream("LPT1");
         PrintStream ps = new PrintStream(fos);
                 ps.print("Your string goes here");
                 ps.print("\f");
                 ps.close();
     } catch (Exception e) {
         System.out.println("Exception occurred: " + e);
     }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

tracypoonAuthor Commented:
Thank you!
But I could not work out with the code, is there any problem with mine?

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.print.*;

public class MyPrint {
            FileOutputStream fos = new FileOutputStream("LPT1");
            PrintStream ps = new PrintStream(fos);
            ps.print("Hello!");
            ps.print("\f");
            ps.close();
}

--------------------------------------
The compilation error is:
MyPrint.java:10: Type expected.
            ps.print("Hello!");
                   ^
1 error

Can you help me again?
0
vladi21Commented:
what about method main?
0
tracypoonAuthor Commented:
I want to implement a very simple task: when I execute the java program, the printer at a particular port prints the string I wants.
Can you suggest me the main method?
0
sgomsCommented:
modify it to,

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.print.*;

public class MyPrint {
     FileOutputStream fos = new FileOutputStream("LPT1");
     PrintStream ps = new PrintStream(fos);
     public static void main(String args[]){  
                ps.print("Hello!");
                ps.print("\f");
                ps.close();
      }
}

-sgoms
0
tracypoonAuthor Commented:
I want to implement a very simple task: when I execute the java program, the printer at a particular port prints the string I wants.
Can you suggest me the main method?
0
tracypoonAuthor Commented:
Thank you very much! I have made it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.