Input stream: file ---> Program
Output stream: Program ----> File
Four abstract streams in Java:
Input stream: InputStream (byte input stream) Reader (character input stream)
Output stream: OutputStream (byte output stream) Writer (character output stream)
Differences:
Text files can only be operated by words for images, audio and video files.
Procedure:
1) obtain the source or target of the IO operation
2) channel for IO operations
3) perform IO operations
4) Disable IO
OutputStream:
Import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. io. outputStream; public class OuputStreamDemo {public static void main (String [] args) throws IOException {// 1. find the target File file = new File ("d:/Test/test.txt") of the stream; // 2. create an output stream pipeline and connect to the target OutputStream OS = new FileOutputStream (file); // 3. the input output // byte output stream can only write data one by one directly. write ('\ n'); for (int I = 0; I <20; I ++) {OS. write (I) ;}string str = "hello, world"; byte [] B = str. getBytes (); OS. write (B); // 4. disable operating OS. close ();}}
InputStream:
Import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream; public class InputStreamDemo {public static void main (String [] args) throws IOException {File file = new File ("D:/Test/cmd.txt "); inputStream is = new FileInputStream (file); byte [] buff = new byte [1024]; int len = 0; while (len = is. read (buff ))! =-1) {String str = new String (buff, 0, len); System. out. println (str);} // garbled characters are displayed in Chinese. The Chinese character is double byte, and the English character is single byte is. close ();}}