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 ();}}