BYTE stream 1. InputStream byte input stream
Code Demo
1InputStream in =system.in;2 3SYSTEM.OUT.PRINTLN ("int read (Byte b) method Demo");4 //int read ()5 intBT = 0;6 while((Bt=in.read ()) >0){ 7System.out.print (bt+ "");8 if(BT = = 10) {//return \ r (13) newline \ n (Ten)9 Break;Ten } One } ASYSTEM.OUT.PRINTLN ("\n\rint read (byte[] buffer) method Demo"); - - //int read (byte[] buffer) the intLength = 0; - byte[] buffer =New byte[10]; - while((length=in.read (buffer))! = 0){ - for(inti=0; i<length; i++){ +System.out.print (buffer[i]+ ""); - } + Break; A } at -SYSTEM.OUT.PRINTLN ("\n\rint read (byte[] buffer, int offset, int len) method Demo"); - - //int read (byte[] buffer, int offset, int len) - intLen = 1024; - intCount = 0; in byte[] buf =New byte[Len]; - while((Count=in.read (buf, 0, Len)) >0){ to for(inti=0; i<count; i++){ +System.out.print (buf[i]+ ""); - } the Break; * } $In.close ();
2. OutputStream byte output stream
Code Demo
1OutputStream out =System.out;2 //void Write (int b)3Out.write (65);//character a4 5Out.write (13);//enter \ r6Out.write (10);//line break \ n7 8 //Flush ()9 Out.flush ();Ten One //write (byte[] bytes) A byte[] bytes =NewString ("Zhang Xiaoming"). GetBytes (); - out.write (bytes); - theOut.write (13);//enter \ r -Out.write (10);//line break \ n - - //write (byte[] bytes, int offset, int length) +bytes =NewString ("Zhangxiaoming"). GetBytes (); -Out.write (Bytes, 5, 8); + AOut.close ();
Character Stream 1. Reader character input stream
Code Demo
1Reader reader =NewInputStreamReader (system.in);2 3 //int read ()4SYSTEM.OUT.PRINTLN ("int Read () method Demo");5 intC;6 while((C=reader.read ())! = 13){7System.out.print ((Char) c);8 }9 Reader.read ();Ten One //int Read (char[] buf) ASYSTEM.OUT.PRINTLN ("\n\rint read (char[] buf) method Demo"); - intCount = 0; - Char[] buf =New Char[1024]; the while((Count=reader.read (BUF)) > 0){ -String str =NewString (buf, 0, count); - if(Str.indexof ("Stop") >=0) Break; - System.out.print (str); + } - + //int read (char[] buffer, int offset, int len) ASYSTEM.OUT.PRINTLN ("\n\rint read (char[] buffer, int offset, int len) method Demo"); at intLength = 1024; - Char[] buffer =New Char[length]; - while((count=reader.read (buffer, 0, length)) > 0){ -String str =NewString (buffer, 0, count); - if(Str.indexof ("Stop") >=0) Break; - System.out.print (str); in}
2. Writer character output stream
Code Demo
1Writer writer =NewOutputStreamWriter (System.out);2String str = "China";3 4 //write (String str) writes a string5 writer.write (str);6 7 //write (int c) writes a single character8Writer.write (10);//line Break9 Ten //Write (string str, int offset, int length) writes a partial string OneWriter.write (str, 0, 1); A -Writer.write (10); - the //Write (char[] buf) writes a character array - Char[] chars =Str.tochararray (); - Writer.write (chars); - +Writer.write (10); - + //Write (char[] buf, int offset, int length) writes a partial character array AWriter.write (chars, 0, 1); atWriter.write (10); - - Writer.flush (); - - //append (char c) Append character -Writer.append (' Z '); inWriter.write (10); - toString str2 = "People's Republic of China"; + //Append (charsequence csq) - writer.append (str2); theWriter.write (10); * $ //Append (charsequence csq, int offset, int length)Panax NotoginsengWriter.append (str2, 0, 4); - theWriter.close ();
"Java Fundamentals summary" IO stream