Common Java classes: I/O system Input and Output

Source: Internet
Author: User

Inputstream is used to represent classes that generate input from different data sources. Outputstream determines the output target.

Classes corresponding to the data source (all inherited from inputstream)

(1) bytearrayinputstream [bytearrayoutputstream]

(2) String object stringbufferinputstream (Deprecated)

(3) file fileinputstream [fileoutputstream]

(4) "Pipeline" pipedinputstream [pipedoutputstream]

(5) sequenceinputstream, a sequence composed of other types of streams

(6) other data sources, such as Internet

 

 

Example:

Package test; import Java. io. bytearrayinputstream; import Java. io. bytearrayoutputstream; import Java. util. arrays;/** bytearrayinputstream (bytearrayoutputstream) indicates generating input (output) from the byte array * this class is actually an operation on a byte array, consider this byte array as a buffer * the method of disabling it is an empty method. If this method is disabled, other methods will not be affected * you can locate the array to the specified position to start reading/writing, the array can be read/written from the beginning, and several bytes in the array can be viewed. * The array can be marked at a certain position, read/write */public class bytearrayinputstreamdemo {public static void main (string [] ARGs) {// input stream buffer (assuming there are already several bytes) byte [] inputbuff = new byte [] {1, 2, 3, 'A', 'B', 'C', 'D', 'E ', 'F'}; byte [] result = new byte [20]; bytearrayinputstream inputstream = new bytearrayinputstream (inputbuff); // read the buffer byte into the result array and output resultinputstream. read (result, 0, 20); system. out. println (arrays. tostring (result); // write the result array to the output stream bytearrayoutputstream outstream = new bytearrayoutputstream (); outstream. write (result, 0, 20); system. out. println (arrays. tostring (outstream. tobytearray ()));}}

 

Package test; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception;/** fileinputstream generates an input stream from the file, and fileoutputstream * outputs the output stream to the file. The read/write, open, and close operations call the local Method */public class fileinputstreamdemo {public static void main (string [] ARGs) throws ioexception {fileinputstream inputstream = NULL; try {inputstream = new fileinputstream (new file ("file/BB. dat ");} catch (filenotfoundexception e) {e. printstacktrace ();} // read a byte array byte [] result = new byte [500]; // int B; // while (B = inputstream. read ())! =-1) // read a byte // system. out. print (char) B); inputstream. read (result); // system. out. println (arrays. tostring (result); inputstream. close (); fileoutputstream outputstream = NULL; // true indicates opening outputstream = new fileoutputstream ("file/BB. dat ", true); // write to outputstream. write (INT) 'A'); outputstream. write (result); outputstream. close ();}}

 

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.