1. Only the output stream has a print stream: PrintWriter and PrintStream, respectively, provide overloaded print,println methods for the output of multiple data types for characters and bytes. PrintWriter and PrintStream operations do not throw exceptions, and the data is not printed or thrown. 2, System.out.print (Object object) is called the ToString () method of the object entity class. 3, PrintWriter and PrintStream have automatic flush function, do not need to call the Flush method manually. * Here's one thing to emphasize: PrintWriter's auto flush function, which calls PrintWriter (Writer out, Boolean AutoFlush) only when the new PrintWriter object, and AutoFlush is set to true to automatically flush, otherwise it will not automatically flush and must be manually printwriter.flush () to successfully print the data. In addition, the PrintWriter method can not only be nested outside the writer character output stream, but also can be nested outside the byte output stream, PrintWriter construction method is as follows:the concrete construction methods of PrintStream are as follows:4, System.out.println (), java.lang.System.out in the Out is the PrintStream type. Practice Applet 1:Package
test.io.print;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream; Public
class printstreamtest { public static
void main (String args[]) {
printstream PS =
null; FileOutputStream fos =
null;
try { fos =
new fileoutputstream ("e:/Technical Learning/java/test/socket/test6.txt" ); PS =
new printstream (FOS); System.
SetOut(PS); //Is no longer console output, but is output to the file pointed to by FileOutputStream. for (
int i=0; i<3000; i++) {
System.
out . Print (i+""); } }
catch (FileNotFoundException e) { e.printstacktrace (); }
finally{
try {
if (PS! = null) { ps.close (); }
if (fos! = null) { fos.close (); } }
catch (IOException e) { e.printstacktrace (); } } }} Practice Applet 2: Enter content from the console and print the console input to a file, similar to loggingPackage Test.io.print;import Java.io.BufferedReader;import Java.io.FileWriter;import java.io.IOException;import Java.io.InputStreamReader;import Java.io.PrintWriter;import java.util.Date; Public class Printwritertest {Public static void Main (String args[]) {BufferedReader bf = new BufferedReader (new InputStreamReader (system.in));String s = null;PrintWriter pw = null;try {The //printwriter construction method sets whether auto flush is set to true to automatically flush, and if true is not set, you must manually call the Flush method to print the contents to a filepw = new PrintWriter (New FileWriter ("e:/Technology Learning/java/test/socket/test7.txt", true), true);While ((S=bf.readline ()) = null) {if (s.equals ("Exit")) {Break ; }System.out.println (s);pw.println ("---------------------------------------");Pw.println (s);//pw.flush (); }pw.println ("-------------" +new Date () + "------------");} catch (IOException e) {e.printstacktrace ();} finally{try {if (bf! = null) {bf.close (); }if (pw = null) {pw.close (); }} catch (IOException e) {e.printstacktrace (); } } }}
Java IO Learning Note (iv) Print flow