Print stream (only the output stream has no input stream)
PrintWriter and PrintStream are output streams, respectively, for characters and bytes
PrintWriter and PrintStream provide overloaded print methods
Println method for output of multiple data types
PrintWriter and PrintStream output operations do not throw an exception and the user gets an error message by detecting an error state
PrintWriter and PrintStream have automatic flush method
Construction Method:
printwriter (file file) creates aNewPrintWriter, without automatic line flushing, with the specified file. PrintWriter (file file, String CSN) creates aNewPrintWriter, without automatic line flushing, with the specified file and charset. PrintWriter (OutputStream out) creates aNewPrintWriter, without automatic line flushing, from a existing outputstream.printwriter (OutputStream out,BooleanAutoFlush) Creates aNewPrintWriter from an existing outputstream.printwriter (String fileName) creates aNewPrintWriter, without automatic line flushing, with the specified file name. PrintWriter (String fileName, string CSN) creates aNewPrintWriter, without automatic line flushing, with the specified file name and charset. PrintWriter (Writer out) creates aNewPrintWriter, without automatic line flushing. PrintWriter (Writer out,BooleanAutoFlush) Creates aNewprintwriter.printstream (file file) creates aNewprint stream, without automatic line flushing, with the specified file. PrintStream (file file, String CSN) creates aNewprint stream, without automatic line flushing, with the specified file and charset. PrintStream (OutputStream out) creates aNewprint stream. PrintStream (OutputStream out,BooleanAutoFlush) Creates aNewprint stream. PrintStream (OutputStream out,BooleanAutoFlush, String encoding) Creates aNewprint stream. PrintStream (String fileName) creates aNewprint stream, without automatic line flushing, with the specified file name. PrintStream (String fileName, string CSN) creates aNewPrint stream, without automatic line flushing, with the specified file name and charset.
Example One
Public Static voidMain (string[] args) {Try{printstream ps=NULL; FileOutputStream Fos=NewFileOutputStream ("D:\\bak\\log.dat"); PS=NewPrintStream (FOS); if(PS! =NULL) {system.setout (PS);//set PS to System.out.println (); no longer output on the console. } intln = 0; for(Charc = 0; C <= 60000; C++) {System.out.print (c+ ""); if(ln++ >= 100) {System.out.println (); ln= 0; } } } Catch(Exception e) {e.printstacktrace (); } }
Results: D:\\bak\\log.dat,log.data file
Example Two,
Public Static voidMain (string[] args) {Try{printstream ps=System.out; BufferedReader BR=NewBufferedReader (NewFileReader ("D:\\bak\\log.dat")); String s=NULL; while((s = br.readline ())! =NULL) {ps.println (s); } br.close (); } Catch(IOException e) {e.printstacktrace (); } }
Results:
Example Three,
Public Static voidMain (string[] args) {Try{String s=NULL; BufferedReader BR=NewBufferedReader (NewInputStreamReader (system.in)); FileWriter FW=NewFileWriter ("D:\\bak\\logfile.log",true);//log PrintWriter pw=NewPrintWriter (FW); while((s = br.readline ())! =NULL) { if("Exit". Equalsignorecase (s)) Break; System.out.println (S.touppercase ()); Pw.println ("--------"); Pw.println (S.touppercase ()); Pw.flush (); } pw.println ("=====" +NewDate () + "======"); Pw.flush (); Pw.close (); } Catch(IOException e) {e.printstacktrace (); } }
Results:
console:1212121212112121212121adasdasdasdasdadasdasdasdasdexit
Files: D:\\bak\\logfile.log,logfile.log file
Java IO (vi)-Print stream