IO Stream classification
- I/O flows based on byte operations: InputStream and OutputStream
- I/O flow based on character manipulation: reader and writer
- I/O streams based on disk operations: File
- Network operation-based I/O Flow: Socket
Generally, the first two of us are more commonly used
File Basic Operations
Public Static void Main(string[] args)throwsioexception{File File =NewFile ("D:/python"); File.mkdir();//folder does not exist just create this folder /*The difference between mkdir () and mkdirs () is that the former creates a folder that does not exist and must requireThe parent path exists, such as "D:/java/app" to create an app folder if Java must exist, otherwise error, while the Mkdirs () method is not required. */ if(file.exists() &&file.isdirectory()) {System. out.println("Python exists"); File file1 =NewFile ("D:/python/spider.txt"); File file2 =NewFile ("D:/python/user.txt"); File1.CreateNewFile(); File2.CreateNewFile(); file[] files = file.Listfiles(); for(File f:files) {System. out.println("file name under the python directory:"+f.GetName ()); System. out.println("The absolute path of the file under the Python directory:"+f.getabsolutepath ());}Else{System. out.println("Python directory does not exist"); } }
BYTE Stream basic operation
Public Static void Main(strin[] args)throwsexception{//must handle the exception of IO stream, otherwise error! FileOutputStream WoS =NewFileOutputStream ("D:/abc.txt");//If the file is not found, it will be created automaticallyString content ="I love Java";byte[] array = content.getBytes();//character stream is written in bytes, and string is converted to byte type Try{WoS.Write(array); }Catch(IOException e) {System. out.println(e); } FileInputStream iOS =NewFileInputStream ("D:/abc.txt");Try{intLen =0;byte[] Array1 =New byte[2];//Two bytes per read while(Len=ios.Read(array1)!=-1) {System. out.println(array1);//Output "[[email protected]"} }Catch(IOException e) {System. out.println(e); }}
Coding:
The computer can only recognize binary data, in bytes; the conversion of text data to binary data is the process of encoding and decoding; The encoding table is composed of characters and their corresponding values, so the character stream = byte stream + encoding table
"你好";//String -- byte[]byte[] bys = s.getBytes// [-60, -29, -70, -61]byte[] bys = s.getBytes("GBK");// [-60, -29, -70, -61]byte[] bys = s.getBytes("UTF-8");// [-28, -67, -96, -27, -91, -67]
Character Stream basic operations
import Java.io.FileInputStream;import Java.io.FileOutputStream;import java.io.IOException;import Java.io.OutputStreamWriter;import Java.io.InputStreamReader; Public classIO { Public Static void Main(String args[])throwsioexception{OutputStreamWriter ows =NewOutputStreamWriter (NewFileOutputStream ("D:/a.txt")); String s ="Troye Sivan"; oWS.Write(s); oWS.Close();//operation of the same file must first close the output streamInputStreamReader IWS =NewInputStreamReader (NewFileInputStream ("D:/a.txt"));Char[] r =New Char[1];intlen=0; while(LEN=IWS.Read(r))! =-1) {System. out.println(r); } IWS.Close(); }}
Simplified notation
import Java.io.FileReader;import Java.io.FileWriter;import java.io.IOException; Public classIo1 { Public Static void Main(string[] args)throwsioexception{FileReader FR =NewFileReader ("D:/a.txt"); FileWriter FW =NewFileWriter ("D:/abc.txt");Char[] ch =New Char[2];intlen=0; while(LEN=FR.Read(CH))! =-1) {FW.Write(CH,0, Len); Fw.Flush(); } fr.Close(); Fw.Close(); }}
Buffered streams
Improve read and write efficiency
import Java.io.FileWriter;import Java.io.FileReader;import Java.io.BufferedReader;import Java.io.BufferedWriter;import java.io.IOException; Public classIo_buffer { Public Static void Main(string[] args)throwsioexception{BufferedWriter bw =NewBufferedWriter (NewFileWriter ("D:/abc.txt"));//bufferedwriter bw = new BufferedWriter (New //OutputStreamWriter (New FileOutputStream ("D:/abc.txt")));Bw.Write("Zayn"); Bw.Write("MALIK"); Bw.Write("Let ME"); Bw.Flush(); Bw.Close(); BufferedReader br =NewBufferedReader (NewFileReader ("D:/abc.txt"));//bufferedreader br = new BufferedReader (New //InputStreamReader (New FileInputStream ("D:/abc.txt")); Char[] ch =New Char[2];intlen=0; while(LEN=BR.Read(CH))! =-1) {System. out.println(NewString (CH,0, Len)); } br.Close(); }}
Summarize:
java specifies that the IO stream must handle an exception or it will error
To read and write data from a file, you use the files class or its subclasses
Java IO Stream