25th section (conversion stream, print stream)

Source: Internet
Author: User

the conversion flow is mainly two inputstreamreader and OutputStreamWriter1. InputStreamReader is mainly the conversion of byte stream input into character input stream .2. OutputStreamWriter is mainly to convert the output stream of byte stream into character output flow Inputstreamreader:outputstreamwriter/*InputStreamReader is mainly the conversion of byte stream input into character input stream .*/import java.io.*; Public classinputstreamreadertest01{ Public Static voidMain (string[] args) {bufferedreader br=NULL; Try{            //byte input stream//FileInputStream fis = new FileInputStream ("C:\\work\\java\\arry.txt"); //character input stream//InputStreamReader ISR = new InputStreamReader (FIS);BR=NewBufferedReader (NewInputStreamReader (NewFileInputStream ("C:\\work\\java\\arry.txt"))); String Temp=NULL;  while(temp = Br.readline ())! =NULL) {System. out. println (temp); }                                } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally{            Try{                if(BR! =NULL) {br.close (); }            } Catch(IOException e) {e.printstacktrace (); }        }                    }}/////////////////////////////////////////////////////////////////*OutputStreamWriter is mainly to convert the output stream of a byte stream into a character output flow*/import java.io.*; Public classoutputstreamwritertest01{ Public Static voidMain (string[] args) {BufferedWriter bw=NULL; Try{                        //byte output stream//FileOutputStream fos = new FileOutputStream ("C:\\work\\java\\arry.txt"); //character output stream//OutputStreamWriter OSW = new OutputStreamWriter (FOS);BW=NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream ("C:\\work\\java\\arry.txt"))); Bw.write ("Zhongshan College");                        Bw.newline (); Bw.write ("I love my country.");                Bw.flush (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally{            Try{                if(BW! =NULL) {bw.close (); }            } Catch(IOException e) {e.printstacktrace (); }        }    }}
The print stream consists of two main: PrintStream and PrintWriter, respectively, corresponding to the byte stream and character stream to complete the screen printing redirect usage with StringBuffer, StringBuilder and StringBuffer is All the methods in StringBuffer are synchronous, thread-safe, but slow and StringBuilder fast, but not thread-safe/*To complete the screen printing redirect System.out corresponds to the PrintStream, the default in the output in the console, we can redirect his output, can be directed to the file is executed System.out.println ("Ming"); Output to a file without outputting to the screen*/import java.io.*; Public classprintstreamtest01{ Public Static voidMain (string[] args) {FileOutputStream fos=NULL; Try{fos=NewFileOutputStream ("C:\\work\\java\\arry.txt"); PrintStream PS=NewPrintStream (FOS);                        System.setout (PS); System. out. println ("Ming is very handsome!"); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally{            Try{                if(Fos! =NULL) {fos.close (); }            }Catch(IOException e) {e.printstacktrace (); }        }    }}////////////////////////////////////////////////////////////////////*system.in can receive input from the screen*/import java.io.*; Public classprintstreamtest02{ Public Static voidMain (string[] args) {bufferedreader br=NULL; Try{InputStreamReader ISR=NewInputStreamReader (System.inch); BR=NewBufferedReader (ISR); String Temp=NULL;  while(temp = Br.readline ())! =NULL) {System. out. println (temp); //if output w exits the loop                if("W". Equals (temp)) {                     Break; }                }        } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally{            Try{                if(BR! =NULL) {br.close (); }            }Catch(IOException e) {e.printstacktrace (); }        }    }}

25th section (conversion stream, 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.