Common Input/output stream case studies in java
Byte input stream:
1. FileInputStream
Purpose: Obtain input bytes from files in the file system. It is often used to read original byte streams such as images and sounds. to read the audio stream, consider using FileReader.
For more information about constructors and common methods, see the API documentation. The Chinese version of API is already available on the Internet. I am an E-literate and cannot afford to hurt.
Here is the most common method:
read(byte[] b, int off, int len)
Maximum number of input streamslen
Bytes of data is read into a byte array.
-> Off: the offset in the B-byte array.
TIPS: array offset, for example, a [1, 2, 3, 4, 5] array. By default, the first array should point to a [0]. If the offset is 2, it will point to a [1].
Case:
Import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; public class Test {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stubtry {int B; // create the input stream FileInputStream readfile = new FileInputStream ("E: \ test.txt"); byte buffer [] = new byte [2500]; // create a byte array // read bytes from the input stream and store them in the buffer array. The maximum length is 2500 bytes. The returned value B is the actual read length B = readfile. read (buffer, 1, 2000); // 1 is the starting offset of the buffer array String str = new String (buffer, 0, B, "Default"); System. out. println (str); System. out. println (B); readfile. close ();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}
There are still many frequently used stream operations to be continued, which will be supplemented later. it's very late. it's time to go to bed. I can do it every day !!