wood burning stoves 2.0*
The moose likes Java in General and the fly likes Printing text file using Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Printing text file using Java" Watch "Printing text file using Java" New topic
Author

Printing text file using Java

Ashim Kumar
Greenhorn

Joined: Mar 13, 2003
Posts: 6
I need to print a text file (say e.g 'bar.txt') from a Java Source Code. Let's say I specify the name of the text file (e.g. 'bar.txt') in my Java Source Code and when I run the Java Code, it should print the text file in the printer. Can anybody tell me, what I need to do? Please specify a block of Code if possible...
Thanks.
Cheers!
Ash
Ashim Kumar
Greenhorn

Joined: Mar 13, 2003
Posts: 6
Yes, friends...I defnitely mean a printer...and not just 'System.out.println'............
Thanks...
Cheers!
Ash
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
This should help (a tutorial):
http://www.javaworld.com/javaworld/jw-10-2000/jw-1020-print.html
Some source code showing how to do it:
http://www.javaworld.com/jw-12-2000/print/jw-1201-print.zip
Ashim Kumar
Greenhorn

Joined: Mar 13, 2003
Posts: 6
The above two links posted by Robert are good for printing but it does not solve my purpose.
What I am looking out is to specify a file name (e.g. 'bar.csf'), might be as an instance of FileOutoutStream and need to print the same from a Java Source Code.
Cheers!
Ashim
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The Print API doesn't specify where the information comes from. If you create an appropriate stream object to read your file, then you can read the info to the stream and use the Print API to print it out.
If you don't know how to open a file stream for reading, then that is a different question entirely.
HTH
Layne
[ March 13, 2003: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Ashim Kumar
Greenhorn

Joined: Mar 13, 2003
Posts: 6
First of all , thank you everybody for all your inputs.
What I have seen from most of you, is that people put suggestions which they haven't themseleves tried it out.
I know, I can send FileOutputStream object for the file and then make the Print API's to print it. That is what I have already suggested. Please don't repeat my suggestion as your reply.

What I need, is a working block of code, which you have already tried out previously. If you have a working block of code to do the same, then please put it up. I will be grateful to you. Please don't repeat my suggestion as your reply.
Thanks everybody.
Cheers!
ASH
Certified by IBM, ORACLE, SUN MICROSYSTEMS
AUDITOR FOR ISO 9001:2000
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Originally posted by Ashim Kumar:
What I have seen from most of you, is that people put suggestions which they haven't themseleves tried it out.

hmmmm . . . Have YOU tried it out? If you won't do it for yourself - why would anyone else? :roll:
If you look at Example2 in the references above, it shows you how to print a String to the printer.
You could just read your file into a String and feed that into the example that you were given.
Do you understand how to read your file into a String??
If so, do you understand where it is that you would modify the example to use your String instead of the one hardcoded in the example?
You were looking for MORE specific than that???


"JavaRanch, where the deer and the Certified play" - David O'Meara
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
What's a CSF file anyway? What type of application uses it? What should the printed output look like? You probably need to find a tool that understands the format of a given file in order to render it visually in an appropriate manner.


"I'm not back." - Bill Harding, Twister
Ashim Kumar
Greenhorn

Joined: Mar 13, 2003
Posts: 6
First of all, I would like to thank you all for your suggestions.
Unfortunately, it doesn't match up with the kind of requirement I have.
Perhaps, it is my mistake that I didn't explain my requirement clearly.
Here it goes, I have a file (say 'bar.csf'). Now this file is similar to any other text file except that it has certain ASCII characters in it. Now the same file is fed to a special barcode printer for printing bar code labels.
All that I need to do is to print the same file from a Java block of code directly WITHOUT any modification or conversions.
Hence if the file is converted into a Swing Component or a Java String doesn't really solve my purpose. Thus even though, the first two suggestion are excellent but doesn't solve my purpose in my case.
All that I need to do is to print the file directly.
Now I did receive some important clue in doing that. I believe JDK 1.4 has javax.print and specially the class 'StreamPrintService' can help me in doing that.
I am digging into that.
Any further suggestions are really welcome.
Thanks once again.
Cheers!
Ash
Kathryn White
Greenhorn

Joined: Mar 16, 2003
Posts: 1
I believe that JDK 1.4 provides support for printing text files directly through java.print API's
There is an example on the Sun Site which explains how to print a text file, images files etc. to a printer.
The link for the same below
http://access1.sun.com/tools/JPSDemo/jpsdemo.html
One more reference to the FORUM:
I think all messages are meant for everybody in general and not specific and personal to anybody. There was no need for Cindy Glass to be personal and be stupid. I hope she doesn't want to scare everybody with her gun !!! (See Profile)
Regards,
Kathy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing text file using Java
 
Similar Threads
Accessing file inside JAR
Source not found
How to access a file from with a jar file?
Printing text file using Java
Printing directly to printer