java input and output streams InputStream / OutputStream

Concept: transfer data between memory and the storage device channel

Stream classification:

1. Press direction:

1.1 input stream: content <storage device> is read into <memory> in

1.2 output stream: content <memory> is written to <storage device> in

2 per unit:

2.1 byte stream: in bytes, can read and write all data

2.2 character stream: in characters, can only read and write text data

3. Press

3.1 Node Stream: a read and write function of the actual transmission of data

3.2 Flow Filter: enhancement stream on the basis of the node

OutputStream Methods:

InputStream Methods:

InputStream / OutputStream are abstract classes, need to subclass new objects

 

FileInputStream:

public int read(byte[] b){}

Reading bytes from the stream, to read the contents of the array memory to b, it returns the number of bytes actually read; reached the end of the file such as peracetic -1


// release resources!
fis.close ();
fos.close ();
}
}

FileOutputStream:

public void write(byte[] b){}

A plurality of write bytes, all bytes in the array b, the output stream

 

 

Case: input from a device into memory, and then output to another device

// assign pictures, upload a copy to the project in coming!
// file in the storage device --- "program to read ----> is written to the storage device


// input stream
fis = new FileInputStream FileInputStream (" C : \\ Users \\ Lenovo \\ \\ 1570252842407.jpg Desktop ");
// output stream
fos = new new a FileOutputStream a FileOutputStream (" messi.jpg ");
// buffer space is too large too small are likely to have little problem
int len = 0; // Representative each read the byte
while ((len = fis.read () ) = - 1!) {// read unless -1
fos.write (len); // how many read and write
}

Published 37 original articles · won praise 105 · Views 6849

Guess you like

Origin blog.csdn.net/S9264L/article/details/104914500