The I/O classes in the Java class Library are divided into two parts for input and output. by inheritance, any class derived from InputStream or reader contains a basic method called Read (), which is used to read a single byte or an array of bytes. Similarly, any class derived from OutputStream or writer contains the basic method named Write (), which is used to write a single byte or byte array.
Here are three subcategories that inherit from InputStream:
First, FileInputStream, FileOutputStream
FileInputStream, which is used to read information from a file. FileOutputStream, used to write information to a file.
The specific code is implemented as follows:
public class InputStream1 {public static void main (string[] args) {String fileName = "D:" +file.separator+ "file.txt"; File File = new file (fileName), if (!file.exists ()) {try {file.createnewfile ();} catch (IOException e) {e.printstacktrace ( );}} FileOutputStream, FileInputStream file operation flow try {outputstream OS = new FileOutputStream (file); String str = "Hello everyone!"; Byte[] A = Str.getbytes (); Os.write (a); Os.close (); InputStream in = new FileInputStream (file); byte[] B = new byte[(int) file . Length ()];in.read (b); In.close (); System.out.println (New String (b));} catch (Exception e) {e.printstacktrace ();}}}
Second, Bytearrayinputstream, Bytearrayoutputstream
Bytearrayinputstream allows memory buffers to be used as inputstream. Bytearrayinputstream creates buffers in memory, and all data sent to "stream" is placed in this buffer.
The specific code is implemented as follows:
Bytearrayinputstream, bytearrayoutputstreamtry {bytearrayoutputstream bos = new Bytearrayoutputstream (); int a = 0;int b = 1; int c = 2;bos.write (a); Bos.write (b); Bos.write (c);//Get data in memory buffer byte[] buf = Bos.tobytearray (); Bos.close (); Bytearrayinputstream bin = new Bytearrayinputstream (buf); int data = 0;while ((Data=bin.read ())!=-1) {System.out.println (data);} Bin.close ();} catch (IOException e) {e.printstacktrace ();}
Three, PipedInputStream, PipedOutputStream
PipedInputStream produces data for writing related PipedOutputStream, realizing the concept of "pipelining". PipedOutputStream any information written to it will automatically be used as the output of the relevant pipedinputstream. Realize the concept of "pipelining".
The specific code is implemented as follows:
PipedInputStream, Pipedoutputstreamsender sender = new Sender (); Receiver receiver = new receiver (); PipedOutputStream OutStream = Sender.getoutstream (); PipedInputStream instream = Receiver.getinstream (); try {//Use the connection () method to connect it. Outstream.connect (instream); } catch (Exception E1) {e1.printstacktrace (); } sender.start (); Receiver.start (); }}class Sender extends thread{private pipedoutputstream outstream = new PipedOutputStream ();p ublic PipedOutputStream get OutStream () {return outstream; public void Run () {String info = "Hello world!"; try {outstream.write (info.getbytes ()); Outstream.close (); } catch (Exception e) {e.printstacktrace (); }}}class Receiver extends Thread {private PipedInputStream instream = new PipedInputStream (); Public PipedInputStream Getinstream () {return instream; } public void Run () {byte[] buf = new byte[1024]; try {instream.read (BUF); SYSTEM.OUT.PRINTLN ("Receive Message from Sender:" + new String (BUF)); InstreAm.close (); } catch (Exception e) {e.printstacktrace (); } } }
InputStream and OutputStream and their subclasses