How To Print Text File In Java

How to code in java to print text file (dotmatrix printer)?

This program should work:

Source code:

package mypackage;
import java.io.*;

public class SimplePrinting {
public static String strLine;
public static String strLine1="";
 

public static void main(String[] args) {
try{
// Open the file that is the first 
// command line parameter
FileInputStream fstream = new FileInputStream("c:\test.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
strLine1 = strLine1 + strLine;
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

try {
FileWriter out = new FileWriter("lpt1");
out.write(strLine1);
out.write(0x0D); // CR
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.