o7planning

Table Of Content

Java PrintWriter Tutorial

View more Tutorials:

1- PrintWriter

PrintWriter is a subclass of Writer, which is used to print formatted data into an OutputStream or another Writer it manages.
  • TODO Link!
  • TODO Link!
PrintWriter​ Constructors
public PrintWriter​(File file)    

public PrintWriter​(File file, String csn)    

public PrintWriter​(File file, Charset charset)    

public PrintWriter​(OutputStream out)    

public PrintWriter​(OutputStream out, boolean autoFlush)    

public PrintWriter​(OutputStream out, boolean autoFlush, Charset charset)    

public PrintWriter​(Writer out)    

public PrintWriter​(Writer out, boolean autoFlush)    

public PrintWriter​(String fileName)    

public PrintWriter​(String fileName, String csn)    

public PrintWriter​(String fileName, Charset charset)
There are quite a few constructors to initialize a PrintWriter object. Let's see what happens when creating a PrintWriter in some specific situations:

PrintWriter​(Writer)

Creates a PrintWriter object to print formatted data into another Writer.

PrintWriter​(OutputStream):

Create a PrintWriter object to print formatted data into an OutputStream.

PrintWriter​(File file) / PrintWriter​(String fileName)  

Creates a PrintWriter object to print formatted data to a file.
Characteristics of PrintWriter:
All methods of PrintWriter do not throw I/O exceptions, to check whether the exception has occurred, you can call checkError() method.
If BufferedWriter participates in structure of PrintWriter, data will be written temporarily to buffer (of BufferedWriter), Which will be pushed down to targert when buffer is full (See illustration above). You can proactively push data into target by calling PrintWriter.flush() method.
If PrintWriter is created with autoFlush feature enabled, data is pushed into target when PrinWriter.Println(..) or PrintWriter.format(..) method is called.
PrintWriter methods:
Methods of PrintWriter
public PrintWriter append​(char c)   
public PrintWriter append​(CharSequence csq)  
public PrintWriter append​(CharSequence csq, int start, int end)   
public PrintWriter format​(String format, Object... args)   
public PrintWriter format​(Locale l, String format, Object... args)  
public PrintWriter printf​(String format, Object... args)  
public PrintWriter printf​(Locale l, String format, Object... args)

public boolean checkError()
public void close()   
public void flush()

protected void clearError()  
protected void setError()

public void print​(boolean b)   
public void print​(char c)   
public void print​(char[] s)
public void print​(double d)  
public void print​(float f)  
public void print​(int i)  
public void print​(long l)  
public void print​(Object obj)
public void print​(String s)  

public void println()  
public void println​(boolean x)  
public void println​(char x)  
public void println​(char[] x)  
public void println​(double x)
public void println​(float x)  
public void println​(int x)  
public void println​(long x)  
public void println​(Object x)
public void println​(String x)  

public void write​(char[] buf)
public void write​(char[] buf, int off, int len)
public void write​(int c)  
public void write​(String s)
public void write​(String s, int off, int len)

2- Examples

PrintWriterEx1.java
package org.o7planning.printwriter.ex;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Locale;

public class PrintWriterEx1 {

    // Windows: C:/SomeFolder/pw-out-test.txt
    private static final String filePath = "/Volumes/Data/test/pw-out-test.txt";

    public static void main(String[] args) throws IOException {
        
        // Create a PrintWriter to write a file.
        PrintWriter printWriter = new PrintWriter(filePath);
        
        LocalDateTime now = LocalDateTime.now();
        
        String empName = "Tran";
        LocalDate hireDate = LocalDate.of(2000, 4, 23);  
        int salary = 10000;
        
        printWriter.printf("# File generated on %1$tA, %1$tB %1$tY %tH:%tM:%tS %n", now, now, now);
 
        printWriter.println(); // New line
        printWriter.printf("Employee Name: %s%n", empName);
        printWriter.printf("Hire date: %1$td.%1$tm.%1$tY %n", hireDate);
        printWriter.printf(Locale.US, "Salary: $%,d %n", salary);

        printWriter.close();
    }
}
Output:
pw-out-test.txt
# File generated on Thursday, February 2021 01:31:22

Employee Name: Tran
Hire date: 23.04.2000
Salary: $10,000
PrintStream.printf(..) and PrintWriter.printf(..) methods are the same in usage, you can refer to its usage in article below:
  • TODO Link!

View more Tutorials:

Maybe you are interested

These are online courses outside the o7planning website that we introduced, which may include free or discounted courses.