Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print txt file

Status
Not open for further replies.

yytan

Programmer
May 8, 2002
113
0
0
MY
dear all;

i am trying to print a txt file from java program, may i know how to do that?

i am really appreciate for any help. thanks!
 
This open a file called "file.txt" for writing to. Hope it helps,


try
{
BufferedWriter out = new BufferedWriter(new FileWriter("/path_to_file/file.txt"));

out.write("whatever");

out.close();
}
catch (IOException ie)
{
System.out.println("whatever");
}

Ryan
 
>> i am trying to print a txt file

If you mean print as on a "Printer" you need to look at the java.awt.print package. Printing in Java is somewhat complex... well actually printing is just complex anyway.

Here is a link to a good introduction article on the use of the print package:


good luck
-pete
 
dear ryan and palbano;

i appreciate for your help, thanks. and i actually got a sample code from sun, which can work, i dont know why. i compiled the program, gave no error, but when i run it give no result.

suppose, i want the program to print the text file on a Printer.
 
this is the simple printing.

import java.awt.*;
import java.io.*;
public class lpt
{

public static void main (String[] argv)
{
//check for argument
if (argv.length != 1)
{
System.out.println(&quot;usage: java lpt <printer name>&quot;);
System.exit(0);
}

try
{

//open printer as if it were a file
FileOutputStream os = new FileOutputStream(argv[0]);
//wrap stream in &quot;friendly&quot; PrintStream
PrintStream ps = new PrintStream(os);

//print text here
ps.println(&quot;Hello world!&quot;);

//form feed -- this is important
//Without the form feed, the text
//will simply sit in the print
//buffer until something else
//gets printed.
ps.print(&quot;\f&quot;);

//flush buffer and close
ps.close();
}
catch (Exception e)
{
System.out.println(&quot;Exception occurred: &quot; + e);
}
}

}
 
dear oracleDevAndMYSQL ;

i am glad to receive ur answer, when i compiled, it gave no error. when i run

if i

run-->java lpt
output-->usage: java lpt <printer name>

if i

run-->java lpt ibm (this is my printer name)
output-->usage: java lpt <printer name>

if i

run-->java lpt test.txt (this is my test file)
output-->nothing

may i know how to run it?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top