Overview
I/O streams are divided into two major categories: character stream and byte stream.
Byte stream (Basic stream)
1, byte input stream class name: FileInputStream feature: Read (read to file) Parent class:InputStream
2, byte output stream class Name: FileOutputStream feature: Write (write to file) parent class: OutputStream
Character stream (Basic stream)
1. Character input stream class name:filereader feature: Read (read to file) Parent class:InputStreamReader
2. Character output stream class name: FileWriter feature: Write (write to file) parent class: OutputStreamWriter
Buffered stream byte buffer stream
1. Buffer byte input stream class name: Bufferedinputstream feature: Read (read to file) Parent: FilterInputStream
2. Buffered bytes output Stream class name:bufferedoutputstream Feature: Write (write to file) parent class: Filteroutputstream
Character Buffer stream
1. Buffered character input stream class name: BufferedReader feature: Read (read to file) Parent: Reader (the basic input stream readLine()
reads one line more than once)
2. Buffered character output stream class name: BufferedWriter feature: Write (write to file) parent class: writer (more than the basic output stream newLine()
line wrapping method)
Convert stream
1, InputStreamReader Features: Read (read the file) input stream parent class: Reader
2. OutputStreamWriter Features: Write (write to file) output stream parent class: Writer
Print Flow
1. PrintStream Features: Write (write to file) bottom: basic byte stream
2. PrintWriter Features: Write (write to file) bottom: basic character stream
The difference between a basic byte stream and a basic character stream:
1, the byte stream can read and write any file character stream only the ordinary file
2, read and write text files as far as possible use character stream this is more efficient than the word throttle
3, read and write media files with a byte stream
4, basic byte throttle no buffer
5. The basic character stream has a buffer default size of 8k (cannot specify buffer size)
6, the byte stream is a low read efficiency of the bytes although can read a variety of files but most suitable for reading media files
7. Character stream is a higher reading efficiency
The difference between a buffered stream and a basic stream:
1, the buffer flow efficiency is higher than the basic flow
2, buffer character stream to make up the basic character stream can not set buffer size gap
3. Buffer byte stream and basic byte stream are recommended to use basic byte stream
4. Buffer character stream is recommended when buffer character stream and basic character stream are used
The role of the conversion stream
The main purpose of the conversion stream is to solve the Chinese characters in the Read and write operation (java default processing file is GBK when the text file is saved as a utf-8 read and write operation will appear in Chinese garbled)
Read and write OutputStreamWriter OSW = new OutputStreamWriter (New FileOutputStream ("A.txt"), "Utf-8"), Osw.write ("China"); o Sw.close (); InputStreamReader ISR = new InputStreamReader (New FileInputStream ("A.txt"), "Utf-8"); char[] cc = new CHAR[10] ; Isr.read (CC); string ss = new string (cc); SYSTEM.OUT.PRINTLN (ss); Isr.close ();
API map
Summary of Usage:
1, the multimedia class uses the byte stream
READ: basic byte input stream
Write: basic byte output stream
2. Character stream for file file
READ: Recommended input stream with character buffer
Write: recommended to buffer the output stream with characters if the data is written in a variety of suggestions with the print stream (PrintStream, PrintWriter)
3. More obvious intent of the data stream
Read-write objects can only be used with object flow (ObjectInputStream, ObjectOutputStream)
If you encounter Chinese characters in the process of reading and writing files, you can only use the conversion stream (InputStreamReader, OutputStreamWriter)
Simple example:
Copy the smaller file code:
Copying smaller files//read-write after the file must exist does not automatically create a write when the file can not exist will automatically create files f = new file ("D:\\practice\\backend\\a.txt"); FileInputStream fis = new FileInputStream (f); byte[] B = new byte[(int) f.length ()]; Fis.read (b); Fis.close ();//write FileOutputStream fos = new FileOutputStream ("D:\\practice\\backend\\b.txt");// Does not exist automatically creates fos.write (b); Fos.close ();
Copy large file code:
Read copy large file//Read side write FileInputStream fis = new FileInputStream ("D:\\practice\\backend\\hello.mp4");// No presence does not automatically create fileoutputstream fos = new FileOutputStream ("D:\\practice\\backend\\h.mp4");//does not exist automatically creates byte[] B = new byte[ 1024*1024];//per read 1Mwhile (true) {int len = Fis.read (b); System.out.println (len); if (len = =-1) {break;//When the return value is -1 proof that the file has finished reading the terminating loop}fos.write (b, 0, Len);} Fis.close (); Fos.close ();
Use recursion to find video files in a directory
public class Demo7 {public static void Findavi (String pathName) {file FF = new File (pathName); file[] FSS = Ff.listfiles (); for (File FA:FSS) {if (Fa.isfile ()) {///determines if the file is if (Fa.getname (). EndsWith (". mp4")) {// Determine if the video file is System.out.println (FA);}} Else{findavi (Fa.getpath ());}}} public static void Main (string[] args) {//recursively find video files under a directory//e:\ thunderbolt download String ss = "e:\\ Thunderbolt download"; Findavi (ss);//static method can only access static method}}
Java I/O flow detailed