Java IO (vi)-Print stream

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.