ホームページ >Java >&#&チュートリアル >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.
Given below are the constructors mentioned:
Given below are the methods:
Given below are the examples mentioned:
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.
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.
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 中国語 Web サイトの他の関連記事を参照してください。