The java PrintWriter class is used to print objects in the formatted representation to the text output stream. The PrintWriter class is a built-in class in java that defines the java.io.PrintWriter package. The Writer class is a superclass of the PrintWriter. The PrintWriter class enables to write the formatted object to the underlying Writer class, for example, to write int, double, long and other primitive object or data as in the text, not in the values of the bytes. The PrintWriter class implements all the print method as the PrintStream except the methods which write the raw bytes. As the PrintWriter class intended to write the text, it is useful to generate reports to mix the numbers and text.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
The following is the declaration for java.io. PrintWriter class:
public class PrintWriter extends Writer { // Constructors and methods of the PrintWriter class }
The above is the syntax of the PrintWriter, where it is extended to the Writer class.
Constructors of Java PrintWriter Class
Given below are the constructors mentioned:
- PrintWriter(File file): These constructs create the new instance of PrintWriter, with the specified file and which is not automat line flushing.
- PrintWriter(File file, String ch): These constructs create the new instance of PrintWriter, with the specified file, charset, and not automatic line flush.
- PrintWriter(OutputStream out): These constructs create the new instance of PrintWriter from an existing OutputStream and which is not automat line flush.
- PrintWriter(OutputStream out, booleanautoFlush): These constructs create the new instance of PrintWriter from an existing OutputStream.
- PrintWriter(String fName): These constructs create the new instance of PrintWriter with the specified file name and which is not automat line flush.
- PrintWriter(String fileName, String csn): These constructs create the new instance of PrintWriter with the specified file name and charset, and it is not automat line flush.
- PrintWriter(Writer out): These constructs create the new instance of PrintWriter, which is not an automat line flush.
- PrintWriter(Writer out, booleanautoFlush): These constructs create the new instance of PrintWriter.
Methods of Java PrintWriter Class
Given below are the methods:
- public void print( Object obj): This method prints an object.
- public void println(boolean x): This method prints the boolean value.
- public void println(char[] x): This method prints an array of characters.
- public void println(int x): This method prints an integer. Similar methods to prints for all different primitive objects.
- public PrintWriterappend(char ch): This method appends the pass character to the writer.
- public PrintWriterappend(CharSequencechseq): This method appends the specified character sequence to the writer.
- public PrintWriterappend(CharSequencech, int start, int end): This method appends a subsequence of pass character to the writer.
- public booleancheckError(): This method checks its error state and flushes the stream.
- public protected void clearError(): This method clears an internal error state of the stream.
- public protected void setError(): This method indicates that an error occurs.
- public PrintWriterformat(String format, Object..args): This method writes the formatted string to this writer with the passed format string and arguments.
- public void flush(): This method flushes the stream.
- public void close(): This method closes the stream.
Examples of Java PrintWriter
Given below are the examples mentioned:
Example #1
Here we create a PrintWriter object by using the PrintWriter class constructor and pass the file name to write in it.
Code:
package p1; import java.io.File; import java.io.PrintWriter; public class Demo { public static void main( String[] arg) { try { //create the new instance of PrintWriter with the specified file PrintWriter pw =null; pw = new PrintWriter(new File("D:\\data.txt")); pw.write("This ia an example text for PrintWriter."+"\n"); pw.write("This ia an example number for PrintWriter :" +(int)563+ "."); pw.flush(); pw.close(); System.out.println("Print writer done. you can open the file."); }catch(Exception e) { System.out.println(e); } } }
Output:
When we open the file, we can see the content of the file as below:
As in the above code, the file “data.txt” is opened to write some data with the help of PrintWriter class and its constructor.
Example #2
Here we create a PrintWriter object of a console that is System.out by using the PrintWriter class constructor and write on the console.
Code:
package p1; import java.io.File; import java.io.PrintWriter; import java.util.Locale; public class Demo { public static void main( String[] arg) { String str="Hello"; String str2=" World"; char ch='H'; try { //create the new instance of PrintWriter to System.out PrintWriter pw =null; pw = new PrintWriter(System.out); pw.print("print(inti) : "+ 123); pw.println(); pw.print("print(float f) : " +34.65f); pw.println(); pw.print("print(boolean b) : " +true); pw.println(); pw.print("print(char ch) : "+ch); pw.println(); pw.print("print(String s) : "+str); pw.println(); pw.print("print(Object Obj) : " +pw); pw.println(); pw.print("append(CharSequencecsq) : "); pw.append(str); pw.append(str2); pw.println(); pw.println("checkError() : " +pw.checkError()); pw.print("format() : "); pw.format(Locale.CHINA, " This is an china formatted example for %s text.",str); pw.flush(); pw.close(); }catch(Exception e) { System.out.println(e); } } }
Output:
As in the above code, writing some of the data we can see in the output to the out console (System.out) with PrintWriter class constructor and methods.
Conclusion
The PrintWriter class is a built-in class in java that is defined in the java.io.PrintWriter package, and it is used to print an object in the formatted representation to the text output stream. The PrintWriter class is the subclass of the Writer class.
The above is the detailed content of Java PrintWriter. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
