Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

printing text to a printer using FileOutputStream

807597Jun 21 2005 — edited Aug 23 2005
Hi,
I'm having problems printing text to a printer.

I'm trying to open a printer device as if it was a file, but it's not working. I'm using a Mac and I am unfamiliar with unix. The printer I am using is a HP LaserJet 1300 that is connected to a usb port. I think that I may not be specifying the device properly. I have searched the web and saw that most people use /dev/usb/lp0 as their printer name if it is connected to a usb port. This does not work for me.

At the unix command line, I typed lpinfo -v and got this:
direct usb://Hewlett-Packard/hp%20LaserJet%201300?serial=00CNCB827826

does that change anything?

Please help me if you can. My code follows...
public void printScores(String printerName) {
		try {
			// open printer as if it was a file
			FileOutputStream printOut = new FileOutputStream(printerName);
			// wrap stream in a "friendly" PrintStream
			PrintStream printStream = new PrintStream(printOut);
			
			// output text
			printStream.println("NAME:  " + username);
			printStream.println("\nPRETEST SCORE");
			printStream.print(pretestToString());
			printStream.print("\nTRAINING SCORES");
			printStream.print(trainingToString());
			printStream.println("\nPOST TEST SCORE");
			printStream.print(posttestToString());
			
			// form feed to push everything out of the buffer
			printStream.print("\f");
			
			// flush the buffer and close it
			printStream.close();
		}
		catch (Exception e) {
			System.out.println("ERROR: subjectData.java -- problem trying to print scores");
		}
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 20 2005
Added on Jun 21 2005
1 comment
311 views