IO byte stream in Java

Source: Internet
Author: User

Order of closing streams:

  * When a depends on B, turn off A and then close B
* Flush is performed once the buffered stream is last closed

bytearrayinputstream : is a byte array input stream, which acts as a byte string (or an array of bytes ) into the form of an input stream

1  PackageObject.io;2 3 ImportJava.io.ByteArrayInputStream;4 ImportJava.io.ByteArrayOutputStream;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 8  Public classBytearraystream {9 Ten      Public Static voidMain (string[] args)throwsIOException { One         //TODO auto-generated Method Stub A         byte[] b=New byte[]{' A ', ' B ', ' C '}; -         //turn byte input into a stream -Bytearrayinputstream input=NewBytearrayinputstream (b); theBytearrayoutputstream output=NewBytearrayoutputstream (); -FileOutputStream fileout=NewFileOutputStream ("D:\\Program Files (x86) \\io\\ByteArray.txt"); -         intlength; -          while((Length=input.read ())!=-1){ + fileout.write (length); -         } +         //flushes the contents of the buffer to the output stream A Output.writeto (fileout); at input.close (); - output.close (); - fileout.close (); -     } -      -  in}

sequenceinputstream : Merging multiple files into one file
1  PackageObject.io;2 3 ImportJava.io.FileInputStream;4 Importjava.io.FileNotFoundException;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 ImportJava.io.SequenceInputStream;8 Importjava.util.ArrayList;9 Importjava.util.Collections;Ten Importjava.util.Enumeration; One Importjava.util.List; A  -  -  Public classSequencestream { the      -     //merging multiple files into one file -      Public Static voidMain (string[] args)throwsIOException { -         //TODO auto-generated Method Stub +      -FileInputStream f1=NewFileInputStream ("D:\\Program Files (x86) \\io\\sequence1.txt"); +FileInputStream f2=NewFileInputStream ("D:\\Program Files (x86) \\io\\sequence2.txt"); AFileInputStream f3=NewFileInputStream ("D:\\Program Files (x86) \\io\\sequence3.txt"); atList<fileinputstream> list=NewArraylist<fileinputstream>(); - List.add (F1); - List.add (F2); - List.add (F3); -          -         //converting a collection to an enumeration type inEnumeration<fileinputstream> e=collections.enumeration (list); -          to         //The enumeration type is received, as shown in +Sequenceinputstream s=NewSequenceinputstream (e); -          theFileOutputStream outputstream=NewFileOutputStream ("D:\\Program Files (x86) \\io\\sequence4.txt"); *         intlength; $          while((Length=s.read ())!=-1){Panax Notoginseng outputstream.write (length); -         } the f1.close (); + f2.close (); A f3.close (); the s.close (); + outputstream.close (); -     } $  $}

Bufferedinputstream : A buffered byte input stream, which is an advanced stream (processing stream) that is used in conjunction with other low-level streams to wrap the other streams together. High efficiency
Bufferedinputstream:
1  PackageObject.io;2 3 ImportJava.io.BufferedInputStream;4 ImportJava.io.BufferedOutputStream;5 ImportJava.io.FileInputStream;6 ImportJava.io.FileOutputStream;7 Importjava.io.IOException;8 9  Public classBufferstream {Ten      Public Static voidMain (string[] args)throwsIOException { OneFileInputStream input=NewFileInputStream ("D:\\Program Files (x86) \\io\\bufferStream1.txt"); ABufferedinputstream binput=Newbufferedinputstream (input); -FileOutputStream output=NewFileOutputStream ("D:\\Program Files (x86) \\io\\bufferStream2.txt"); -Bufferedoutputstream boutput=Newbufferedoutputstream (output); the         intlength; -          while((Length=binput.read ())!=-1){ - boutput.write (length); -         } +         //flushes the contents of a buffer to a file - Boutput.flush (); + input.close (); A binput.close (); at output.close (); - boutput.close (); -     } -}

IO byte stream in Java

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.