PrintWriter is a print output stream of character types, inherited from writer, used to print the formatted representation of an object to the text output stream.
Main functions of PrintWriter:
PrintWriter (OutputStream out) PrintWriter (OutputStream out, Boolean AutoFlush) PrintWriter (Writer wr) PrintWriter ( Writer wr, Boolean AutoFlush) printwriter (file file) printwriter (file file, string CSN) PrintWriter (string fileName) Print Writer (String fileName, string CSN) PrintWriter append (char c) printwriter append (charsequence csq, int start, in T end) PrintWriter append (Charsequence csq) boolean checkerror () void close () void flush () printwriter Format (Locale l, string format, Object ... args) printwriter format (string format, object ... args) void print (Floa T fnum) void print (double dnum) void print (String str) void print (Object obj) void print (char ch) void Print (char[] chararray) void print (long lnum) void print (int inum) void print (boolean bool) PrintWriter p rintf (Locale L, string format, Object ... args) printwriter printf (string format, Object ... args) void println () void println (float f) void PRINTLN (int i) void println (long L) void println (Object obj) void println (char[] chars) void println (String s TR) void println (char c) void println (double D) void println (Boolean b) void Write (char[] buf, int offset, int count) void write (int onechar) void write (char[] buf) void write (String str, int offset, int count) void Write (String str)
Sample code:
public class Printwritertest {public static void main (string[] args) {//The function of the following 3 functions is the same: the letter "ABCDE" is written to the file "F"
Ile.txt ".
Choose one of the execution options.
TestPrintWriterConstrutor1 ();
TestPrintWriterConstrutor2 ();
TestPrintWriterConstrutor3 ();
Test write (), print (), println (), printf (), and so on.
Testprintwriterapis (); The function of the test function */** * PrintWriter (OutputStream out) is to write the letter "ABCDE" to the file "file.txt"/private
static void TestPrintWriterConstrutor1 () {final char[] arr={' A ', ' B ', ' C ', ' d ', ' e '};
try {///create File "file.txt" Files Object file ("file.txt");
Create file corresponds to FileOutputStream printwriter out = new PrintWriter (new FileOutputStream (file));
Writes "byte array arr" to the output stream Out.write (arr);
Turn off the output stream out.close ();
catch (IOException e) {e.printstacktrace (); }
}
/**
* The function of the test function * PrintWriter (file file) is to write the letter "ABCDE" to the file "file.txt"/Private St
atic void TestPrintWriterConstrutor2 () {final char[] arr={' A ', ' B ', ' C ', ' d ', ' e '};
try {File File = new file ("file.txt");
PrintWriter out = new PrintWriter (file);
Out.write (arr);
Out.close ();
catch (IOException e) {e.printstacktrace ();
}/** * PrintWriter (String fileName) is the function of the test function * function, which is to write the letter "ABCDE" to the file "file.txt"
* * private static void TestPrintWriterConstrutor3 () {final char[] arr={' A ', ' B ', ' C ', ' d ', ' e '};
try {printwriter out = new PrintWriter ("file.txt");
Out.write (arr);
Out.close ();
catch (IOException e) {e.printstacktrace (); }/** * Test write (), print (), println (), An interface such as printf ().
*/private static void Testprintwriterapis () {final char[] arr={' A ', ' B ', ' C ', ' d ', ' e '};
try {//create file corresponds to FileOutputStream printwriter out = new PrintWriter ("Other.txt");
Writes the string "Hello PrintWriter" + carriage return to the output stream out.println ("Hello PrintWriter");
Writes 0x41 to the output stream//0x41 corresponds to the ASCII letter ' A ', which is written to the character ' a ' out.write (0x41);
Writes the string "65" to the output stream. Out.print (0x41);
Equivalent to Out.write (string.valueof (0x41));
Out.print (0x41);
Appends the character ' B ' to the output stream out.append (' B '). Append ("Cdef");
Write the "CDE is 5" + carriage return to the output stream String str = "GHI";
int num = 5;
out.printf ("%s is%d\n", str, num);
Out.close ();
catch (IOException e) {e.printstacktrace (); }
}
}