Public classBytestreamdemo {/*int available (); The size of the input file can be obtained (number of bytes) without returning 0 void close (); Close input Stream abstract int read (); Reads a byte and returns the read byte, without returning-1 int read (byte[] b); Reads the contents into a byte array, and returns the number of bytes read in. , no return-1 int read (byte[] b, int off, int len); Reads the contents into a byte array, reads from off, and reads Len ends. , no return-1*/ /*Public void Close (); Closes the output stream. public void flush (); Refreshes the buffer. public void Write (byte[] b); Writes a byte array to the data stream write (byte[] b, int off, int len); Writes data from the specified range to the data stream. public abstract void Write (int b); Writes a byte of data to the data stream*/ //the input stream does not find a datagram exception, and the output stream does not automatically create a file Public Static voidExecutebytefile () {File inFile=NewFile ("D:" +file.separator+ "Intest.txt"); File OutFile=NewFile ("D:" +file.separator+ "Outtest.txt"); LongStart =System.currenttimemillis (); FileInputStream in=NULL; OutputStream out=NULL; Try{ in=NewFileInputStream (InFile); //True indicates that the original file is appended. out =NewFileOutputStream (OutFile,true); byte[] inb =New byte[1024]; intSize = 0; while(size = In.read (INB))! = 1) {//System.out.println (New String (Inb,0,size, "GBK"));Out.write (INB, 0, size); } } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally{closeutil.close (out); Closeutil.close (in); } System.out.println ("End Time:" + (System.currenttimemillis ()-start)); } /**manipulating the input stream of a byte array *@throwsIOException*/ Public Static voidExecutebytein ()throwsioexception{FinalString str = "I love China!"; byte[] bytes =str.getbytes (); Bytearrayinputstream Byin=Newbytearrayinputstream (bytes); byte[] db =New byte[3]; intRead =byin.read (DB); while(Read!=-1) {String s=NewString (Db,0,read, "UTF-8"); System.out.println (s); Read=byin.read (DB); } byin.close (); } /**manipulating the output stream of a byte array *@throwsIOException*/ Public Static voidExecutebyteout ()throwsioexception{FinalString str1 = "I love China!"; FinalString str2 = "byte array output stream!"; byte[] Bytes1 =str1.getbytes (); byte[] Bytes2 =str2.getbytes (); Bytearrayoutputstream out=NewBytearrayoutputstream (); Out.write (BYTES1); Out.write (Bytes2); //the data is written into the byte array output stream first. After the unified outputSystem.out.println (out.tostring ()); Out.close (); } /**Pipe Flow*/ Public Static voidExecutepip ()throwsioexception{Pipedbyteout out=Newpipedbyteout (); Pipedbytein in=NewPipedbytein (); Out.getout (). Connect (In.getin ()); Thread Inthread=NewThread (In, "thread-piped-in")); Thread Outthread=NewThread (out, "thread-piped-out")); Inthread.start (); Outthread.start (); } /**Cache byte Stream*/ Public Static voidExecutebufferstream ()throwsexception{File inFile=NewFile ("D:" +file.separator+ "Intest.txt"); File OutFile=NewFile ("D:" +file.separator+ "Outtest.txt"); LongStart =System.currenttimemillis (); Bufferedinputstream Inbuffer=NewBufferedinputstream (NewFileInputStream (inFile)); Bufferedoutputstream Outbuffer=NewBufferedoutputstream (NewFileOutputStream (OutFile,true)); byte[] inb =New byte[1024]; intSize = 0; while(size = Inbuffer.read (INB))! = 1) {outbuffer.write (INB,0, size); } outbuffer.flush (); Closeutil.close (Outbuffer); Closeutil.close (Inbuffer); System.out.println ("End Time:" + (System.currenttimemillis ()-start)); } /**object flow can serialize an object*/ Public Static classserializeutil{ Public Static byte[] SerializeObject (Object object) {ObjectOutputStream out=NULL; Bytearrayoutputstream by=NewBytearrayoutputstream (); Try{ out=NewObjectOutputStream (by); Out.writeobject (object); returnBy.tobytearray (); } Catch(IOException e) {Throw NewRuntimeException ("Object serialization Error"); }finally{closeutil.close (by); Closeutil.close (out); } } Public Static<T> T unserialized (byte[] by) { if(by = =NULL|| By.length = = 0)return NULL; Bytearrayinputstream Byin=NULL; ObjectInputStream in=NULL; Try{Byin=NewBytearrayinputstream (by); Inch=NewObjectInputStream (Byin); return(T) in.readobject (); } Catch(IOException |classnotfoundexception e) { Throw NewRuntimeException ("Object deserialization error"); }finally{closeutil.close (in); Closeutil.close (Byin); } } } Public Static voidMain (string[] args)throwsexception{User User=NewUser (); User.setaddres (Address); User.setid (1); User.setname (Test); byte[] bs =serializeutil.serializeobject (user); Object Object=serializeutil.unserialized (BS); System.out.println (object); }}classPipedbyteinImplementsrunnable{PrivatePipedInputStream in =NewPipedInputStream (); @Override Public voidrun () {Try { byte[] by =New byte[1024]; inti =In.read (by); while(I! =-1) {System.out.println (NewString (By,0,i, "UTF-8")); I=In.read (by); } } Catch(IOException e) {e.printstacktrace (); }finally{closeutil.close (in); } } PublicPipedInputStream Getin () {returnIn ; }}classPipedbyteoutImplementsrunnable{PrivatePipedOutputStream out =NewPipedOutputStream (); @Override Public voidrun () {byte[] bytes = "I love China!". GetBytes (); Try{out.write (bytes); } Catch(IOException e) {e.printstacktrace (); }finally{closeutil.close (out); } } PublicPipedOutputStream getout () {returnOut ; }}classUserImplementsserializable{Private Static Final LongSerialversionuid = 1L; PrivateInteger ID; PrivateString name; //transient indicates that the field is not serialized Private transientString addres; PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString getaddres () {returnaddres; } Public voidsetaddres (String addres) { This. addres =addres; } @Override PublicString toString () {return"User [id=" + ID + ", name=" + name + ", addres=" + addres + "]"; }}classcloseutil{ Public Static voidClose (closeable closeable) {if(Closeable! =NULL){ Try{closeable.close (); } Catch(IOException e) {e.printstacktrace (); } } }}
IO (c) byte throttle exercise