Java PrintWriter console Output

Introduction

We can do console output with print() and println() from class PrintStream.

PrintStream is the type of object referenced by System.out.

The following code uses write() to output the character "A" followed by a newline to the screen:

// Demonstrate System.out.write(). 
public class Main { 
  public static void main(String args[]) { 
    int b; /*from  ww w . jav a 2 s .c o m*/
 
    b = 'A'; 
    System.out.write(b); 
    System.out.write('\n'); 
  } 
}

Console and PrintWriter Class

PrintWriter supports the print() and println() methods.

We can use these methods the same way as System.out.

The following application illustrates using a PrintWriter to handle console output:

// Demonstrate PrintWriter 
import java.io.PrintWriter; 
 
public class Main { 
  public static void main(String args[]) { 
    PrintWriter pw = new PrintWriter(System.out, true); 
 
    pw.println("This is a string"); 
    int i = -7; //w  w  w.ja  v  a2  s  .c o m
    pw.println(i); 
    double d = 4.5e-7; 
    pw.println(d); 
  } 
}



PreviousNext

Related