in , ,

Java PrintWriter Class

Java PrintWriter Class
Java PrintWriter Class

In this tutorial, we will learn about Java PrintWriter and its print() and printf() techniques with the help of examples.

The PrintWriter class of the java.io package can be used to write output data in a normally readable structure (text).

It extends the abstract class Writer.


Working of PrintWriter

In contrast to different writers, PrintWriter changes over the primitive data (int, float, char, and so forth) into the content arrangement. It at that point writes that designed data to the writer.

Additionally, the PrintWriter class doesn’t toss any input/output exception. Instead, we need to use the checkError() technique to discover any error in it.

Note: The PrintWriter class likewise has an element of auto flushing. This implies it forces the writer to write all data to the objective if one of the println() or printf() techniques is called.


Make a PrintWriter

To make a print writer, we should import the java.io.PrintWriter package first. When we import the package here is how we can make the print writer.

  1. Using other writers
// Creates a FileWriter
FileWriter file = new FileWriter("output.txt");

// Creates a PrintWriter
PrintWriter output = new PrintWriter(file, autoFlush);

Here,

  • we have made a print writer that will write data to the file represented by the FileWriter
  • autoFlush is an optional parameter that specifies whether to perform auto flushing or not

2. Using other output streams

// Creates a FileOutputStream
FileOutputStream file = new FileOutputStream("output.txt");

// Creates a PrintWriter
PrintWriter output = new PrintWriter(file, autoFlush);

Here,

we have made a print writer that will write data to the record represented to by the FileOutputStream

the autoFlush is a discretionary parameter that indicates if to perform auto flushing or not

3. Using filename

// Creates a PrintWriter
PrintWriter output = new PrintWriter(String file, boolean autoFlush);

Here,

  • we have made a print writer that will write data to the predetermined record
  • the autoFlush is a discretionary boolean parameter that indicates whether to perform auto flushing or nor

Note: In all the above cases, the PrintWriter writes data to the record using some default character encoding. However, we can indicate the character encoding (UTF8 or UTF16) too.

// Creates a PrintWriter using some character encoding
PrintWriter output = new PrintWriter(String file, boolean autoFlush, Charset cs);

Here, we have used the Charset class to specify the character encoding. To know more, visit Java Charset (official Java documentation).


Strategies for PrintWriter

The PrintWriter class gives different techniques that allow us to print data to the output.

print() Method

  • print() – prints the predefined data to the writer
  • println() – prints the information to the essayist alongside another line character toward the end

For example,

import java.io.PrintWriter;

class Main {
  public static void main(String[] args) {

    String data = "This is a text inside the file.";

    try {
      PrintWriter output = new PrintWriter("output.txt");

      output.print(data);
      output.close();
    }
    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

In the above example, we have made a print writer named output. This print writer is connected with the file output.txt.

PrintWriter output = new PrintWriter("output.txt");

To print data to the record, we have used the print() strategy.

Here when we run the program, the output.txt record is loaded up with the accompanying substance.

This is a text inside the file.

printf() Method

The printf() strategy can be used to print the designed string. It incorporates 2 parameters: organized string and contentions. For instance,

printf("I am %d years old", 25);

Here,

  • I am %d years old is a formatted string
  • %d is integer data in the formatted string
  • 25 is an argument

The arranged string incorporates both content and data. Furthermore, the contentions supplant the data inside the organized string.

Hence the %d is replaced by 25.


Example: printf() Method using PrintWriter

import java.io.PrintWriter;

class Main {
  public static void main(String[] args) {

    try {
      PrintWriter output = new PrintWriter("output.txt");

      int age = 25;

      output.printf("I am %d years old.", age);
      output.close();
    }
    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

In the above example, we have made a print writer named output. The print writer is connected with the file output.txt.

PrintWriter output = new PrintWriter("output.txt");

To print the arranged content to the file, we have used the printf() strategy.

Here when we run the program, the output.txt record is loaded up with the accompanying substance.

I am 25 years old.

Other Methods Of PrintWriter

MethodDescription
close()closes the print writer
checkError()checks if there is an error in the writer and returns a boolean result
append()appends the specified data to the writer

To learn more, visit Java PrintWriter (official Java documentation).


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

dynamic-wallpapers-macos-mojave-24-hour-wallpaper

Best Sites to Download Dynamic Wallpapers for Mac

Prevent Your PC from Making Random USB Noises

Prevent Your PC from Making Random USB Noises