DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Trending

  • Creating a Web Project: Caching for Performance Optimization
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  • Navigating Change Management: A Guide for Engineers
  • Memory-Optimized Tables: Implementation Strategies for SQL Server
  1. DZone
  2. Coding
  3. Java
  4. Java PrintWriter With Example

Java PrintWriter With Example

By 
Shatakshi Dixit user avatar
Shatakshi Dixit
·
Feb. 19, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
48.2K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will look at one of the important classes of Java version 7, PrintWriter. Let's get started.

Introduction

The PrintWriter class in Java was released in Java 7, as a subclass of the Writer class. This class is basically used for printing the formatted representations of objects to a text-output stream. 

The PrintWriter class implements all the methods of the PrintStream class. However, this class does not have any methods that are used for writing raw bytes. 

Note: the PrintWriter class is also used for write a file in Java.

PrintWriter class in Java

PrintWriter class in Java

The PrintWriter class has some differences when compared to the PrintStream class. In the PrintStream class, when automatic flushing is enabled, output will be sent when the newline character is the output.

But, in the PrintWriter class, when automatic flushing is enabled, ouput will be printed when the following methods are invoked: println, printf, etc.

Basically, the above-mentioned methods use the platform's notion of a line separator, not a new-line character. One important thing about the PrintWriter class is that it never throws any I/O exceptions. However, its constructor does in case of an error.

This class has a method checkError(), which can be invoked by the client to check whether any error is occurred.

You may also like: Java 8 (A Comprehensive Look): Part 1.1 - Lambdas Under the Hood

Methods of PrintWriter Class

Method Description
void println(boolean x) This is used to print the boolean value provided in the method parameter.
void println(char[] x) This method is used to print an array of characters provided in the parameter.
void println(int x) This is used for printing an integer value.
PrintWriter append(char c) This method appends the specified character to the writer.
PrintWriter append(CharSequence ch) This method appends the specified character sequence to the writer.
PrintWriter append(CharSequence ch, int start, int end) This method is used for appending a subsequence of specified character to the writer.
boolean checkError() This method is used to flushes the stream and check its error state.
protected void setError() This method indicates that an error occurs.
protected void clearError() This method is used to clear all the errors.
PrintWriter format(String format, Object... args) This method is used for writing a formatted string to the writer using specified arguments and format string.
void print(Object obj) This method simply prints the object.
void flush() This method flushes the stream.
void close() This is to close the stream.


PrintWriter Example

Java
 




x
26


 
1
package com.dzone;  
2
  
3
import java.io.File;  
4
import java.io.PrintWriter;  
5
 
          
6
 
          
7
public class PrintWriterTest {  
8
 
          
9
 
          
10
    public static void main(String[] args) throws Exception {  
11
    
12
      PrintWriter writerObj = new PrintWriter(System.out);  
13
      
14
      
15
      writerObj.write("Dzone article");        
16
      writerObj.flush();  
17
      writerObj.close();  
18
 
          
19
      PrintWriter writerObj1 =null;     
20
      
21
         writerObj1 = new PrintWriter(new File("C:\\main.txt"));  
22
         writerObj1.write("Dzone Line number 2");                                                   
23
         writerObj1.flush();  
24
         writerObj1.close();  
25
    }  
26
}  



Further Reading

  • Modern Java.
Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!