The Artima Developer Community
Sponsored Link

Java Answers Forum
print string using PrintStream Class & without System Class

1 reply on 1 page. Most recent reply: Nov 27, 2003 3:17 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Joy

Posts: 15
Nickname: papai
Registered: Nov, 2003

print string using PrintStream Class & without System Class Posted: Nov 25, 2003 6:45 PM
Reply to this message Reply
Advertisement
hello i am a begginner to java world.
I want to have print "abcd" by using PrintStream Class
println method & without System class.
Is It Possible?If how?
with regards
bye.


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: print string using PrintStream Class & without System Class Posted: Nov 27, 2003 3:17 PM
Reply to this message Reply
You create a PrintStream object with a constructor which takes an OutputStream object as its argument:

PrintStream
public PrintStream(OutputStream out)Create a new print stream. This stream will not flush automatically.

Parameters:
out - The output stream to which values and objects will be printed

OutputStream out = System.out;
PrintStream ps = new PrintStream(out);

to make the print stream object print something, you call its print method:
print
public void print(String s)Print a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Parameters:
s - The String to be printed

ps.print("abcd");


or the println method:

ps.println("abcd");
Since the print stream will not automatically flush yo must call the print stream's flush method:

ps.flush();

and then close the print stream if you are done with it:

ps.close();

Summary:


OutputStream out = System.out;
PrintStream ps = new PrintStream(out);
ps.print("abcd");
// or
ps.println("abcd");
ps.flush();
ps.close();

If you are not allowed to use the System.out OutputStream, you could could construct another OutputStream, you could create any of the subclasses of OutputStream such as FileOutputStream.

FileOutputStream fos = new FileOutputStream("test.txt");
PrintStream ps = new PrintStream(fos);
ps.print("abcd");
// or
ps.println("abcd");
ps.flush();
fos.close();


Usually you need to put these lines inside a try catch block to check for IOExceptions

Flat View: This topic has 1 reply on 1 page
Topic: Prob Reg. - MySql with JRun for EJB 2.0 Previous Topic   Next Topic Topic: Need Help in Jsp

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use