AdSense

AdSense3

Thursday, 13 November 2014

java.io.PrintStream class:

The PrintStream class provides methods to write data to another stream. The PrintStream class automatically flushes the data so there is no need to call flush() method. Moreover, its methods don't throw IOException.

Commonly used methods of PrintStream class:

There are many methods in PrintStream class. Let's see commonly used methods of PrintStream class:
  • public void print(boolean b): it prints the specified boolean value.
  • public void print(char c): it prints the specified char value.
  • public void print(char[] c): it prints the specified character array values.
  • public void print(int i): it prints the specified int value.
  • public void print(long l): it prints the specified long value.
  • public void print(float f): it prints the specified float value.
  • public void print(double d): it prints the specified double value.
  • public void print(String s): it prints the specified string value.
  • public void print(Object obj): it prints the specified object value.
  • public void println(boolean b): it prints the specified boolean value and terminates the line.
  • public void println(char c): it prints the specified char value and terminates the line.
  • public void println(char[] c): it prints the specified character array values and terminates the line.
  • public void println(int i): it prints the specified int value and terminates the line.
  • public void println(long l): it prints the specified long value and terminates the line.
  • public void println(float f): it prints the specified float value and terminates the line.
  • public void println(double d): it prints the specified double value and terminates the line.
  • public void println(String s): it prints the specified string value and terminates the line./li>
  • public void println(Object obj): it prints the specified object value and terminates the line.
  • public void println(): it terminates the line only.
  • public void printf(Object format, Object... args): it writes the formatted string to the current stream.
  • public void printf(Locale l, Object format, Object... args): it writes the formatted string to the current stream.
  • public void format(Object format, Object... args): it writes the formatted string to the current stream using specified format.
  • public void format(Locale l, Object format, Object... args): it writes the formatted string to the current stream using specified format.

Example of java.io.PrintStream class:

In this example, we are simply printing integer and string values.
  1. import java.io.*;  
  2. class PrintStreamTest{  
  3.  public static void main(String args[])throws Exception{  
  4.   
  5.    FileOutputStream fout=new FileOutputStream("mfile.txt");  
  6.    PrintStream pout=new PrintStream(fout);  
  7.    pout.println(1900);  
  8.    pout.println("Hello Java");  
  9.    pout.println("Welcome to Java");  
  10.    pout.close();  
  11.    fout.close();  
  12.       
  13.  }  
  14. }   

Example of printf() method of java.io.PrintStream class:

Let's see the simple example of printing integer value by format specifier.
  1. class PrintStreamTest{  
  2.  public static void main(String args[]){  
  3.    int a=10;  
  4.    System.out.printf("%d",a);//Note, out is the object of PrintStream class  
  5.       
  6.  }  
  7. }   
Output:10

No comments:

Post a Comment