Wednesday 1 July 2015

Print writer


Print writer


Image result for printwriter class in java

public class PrintWriter extends Writer

The Java.io.PrintWriter class prints formatted representations of objects to a text-output stream.

Class declaration

Following is the declaration for Java.io.PrintWriter class:
public class PrintWriter
  extends Writer

Field

Following are the fields for Java.io.PrintWriter class:
  • protected Writer out -- This is the character-output stream of this PrintWriter.
  • protected Object lock -- This is the object used to synchronize operations on this stream.

Class constructors

S.N.Constructor & Description
1
PrintWriter(File file)
This creates a new PrintWriter, without automatic line flushing, with the specified file.
2
PrintWriter(File file, String csn)
This creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
3
PrintWriter(OutputStream out)
This creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
4
PrintWriter(OutputStream out, boolean autoFlush)
This creates a new PrintWriter from an existing OutputStream.
5
PrintWriter(String fileName)
This creates a new PrintWriter, without automatic line flushing, with the specified file name.
6
PrintWriter(String fileName, String csn)
This creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.
7
PrintWriter(Writer out)
This creates a new PrintWriter, without automatic line flushing.
8
PrintWriter(Writer out, boolean autoFlush)
This creates a new PrintWriter.

Class methods

S.N.Method & Description
1
This method appends the specified character to this writer.
2
This method appends the specified character sequence to this writer.
3
This method appends a subsequence of the specified character sequence to this writer.
4
This method flushes the stream if it's not closed and checks its error state.
5
This method Clears the error state of this stream.
6
This method Closes the stream and releases any system resources associated with it.
7
This method Flushes the stream.
8
This method writes a formatted string to this writer using the specified format string and arguments.
9
This method writes a formatted string to this writer using the specified format string and arguments.
10
This method prints a boolean value.
11
This method prints a character.
12
This method Prints an array of characters.
13
This method Prints a double-precision floating-point number.
14
This method prints a floating-point number.
15
This method prints an integer.
16
This method prints a long integer.
17
This method prints an object.
18
This method prints a string.
19
This is a convenience method to write a formatted string to this writer using the specified format string and arguments.
20
This is a convenience method to write a formatted string to this writer using the specified format string and arguments.
21
This method terminates the current line by writing the line separator string.
22
This method prints a boolean value and then terminates the line.
23
This method prints a character and then terminates the line.
24
This method prints an array of characters and then terminates the line.
25
This method prints a double-precision floating-point number and then terminates the line.
26
This method prints a floating-point number and then terminates the line.
27
This method prints an integer and then terminates the line.
28
This method prints a long integer and then terminates the line.
29
This method prints an Object and then terminates the line.
30
This method prints a String and then terminates the line.
31
This method indicates that an error has occurred.
32
This method writes an array of characters.
33
This method writes a portion of an array of characters.
34
This methodWrites a single character.
35
This method writes a string.
36
This method writes a portion of a string.
Example,

import java.io.*;
import java.util.Date;

public class PrintWriterDemo {

   public static void main(String[] args) {
      Object obj1 = "Object";
      Object obj2 = 2;
      Date date = new Date(112, 2, 21);
      try {

         // create a new writer
         PrintWriter pw = new PrintWriter(System.out);

         // print object
         pw.println(obj1);

         // print another object
         pw.println(obj2);

         // print a date (it is an object)
         pw.print(date);

         // flush the writer
         pw.flush();

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}
Let us compile and run the above program, this will produce the following result:
Object
2
Wed Mar 21 00:00:00 EET 2012

No comments:

Post a Comment