Java PrintStream class

Introduction

PrintStream defines several constructors.

PrintStream(OutputStream outputStream)  
PrintStream(OutputStream outputStream, boolean autoFlushingOn)  
PrintStream(OutputStream outputStream, boolean autoFlushingOn String  charSet)  
    throws UnsupportedEncodingException 

PrintStream(File outputFile) throws FileNotFoundException  
PrintStream(File outputFile, String charSet)  
    throws FileNotFoundException, UnsupportedEncodingException  
PrintStream(String outputFileName) throws FileNotFoundException  
PrintStream(String outputFileName, String charSet) throws FileNotFoundException,  
    UnsupportedEncodingException 
// Demonstrate printf(). 
 
public class Main { 
  public static void main(String args[]) { 
    System.out.println("Here are some numeric values " + 
                      "in different formats.\n"); 
 
    System.out.printf("Various integer formats: "); 
    System.out.printf("%d %(d %+d %05d\n", 3, -3, 3, 3); 
 
    System.out.println(); /*from   w  w w .ja v a2s  .  c  o  m*/
    System.out.printf("Default floating-point format: %f\n", 
                      1234567.123); 
    System.out.printf("Floating-point with commas: %,f\n", 
                      1234567.123); 
    System.out.printf("Negative floating-point default: %,f\n", 
                      -1234567.123); 
    System.out.printf("Negative floating-point option: %,(f\n", 
                      -1234567.123); 
 
    System.out.println(); 
 
    System.out.printf("Line up positive and negative values:\n"); 
    System.out.printf("% ,.2f\n% ,.2f\n", 
                      1234567.123, -1234567.123); 
  } 
}

PrintStream also defines the format() method. It has these general forms:

PrintStream format(String fmtString , Object ... args)  
PrintStream format(Locale loc, String fmtString , Object ... args) 



PreviousNext

Related