Java 打印编写器
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.
以上是Java 打印编写器的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。
