The school's online courses continue to be in the preparation stage, so I have to put my study aside first. I can only be lucky when I have spare time to learn. Alas, it's too fast
I have learned the related classes of IO flow in these two days.
InputStream/OutputStream (byte stream) and Reader/writer (character stream) classes are abstract parent classes of all IO stream classes. It is necessary for us to have a brief understanding.
1, InputStream and FileInputStream
InputSteam is an abstract class, which cannot be instantiated. It is a super class representing all classes of Byte input stream. All streams inherited from InputSteam are used to input data (read) into the program, and the unit of data is Byte (English Byte, short for B).
Common methods:
FileInputStream reads files in byte mode, suitable for reading all types of files (image, video, text files, etc.). As like as two peas in InputStream, the common method of FileInputStream is not understood, and the level is not enough.
Test it:
package cn.zjb.test; import java.io.FileInputStream; import java.io.IOException; /** * Test file input byte stream * @author Zhang Jian Bo * */ public class TestInputStream { private static FileInputStream in; private static final int DEFAULT_CAPACITY=1024; private static void printContent1(String src) throws IOException { /* * Known byte size in file */ in=new FileInputStream(src); for(int i=0;i<10;i++) System.out.print(in.read()+" "); System.out.println(); in.close(); //If it is not closed, there may be errors. Be sure to close the corresponding stream } private static void printContent2(String src) throws IOException{ /* * Whether you know the number of bytes in the file or not */ in=new FileInputStream(src); int temp; while((temp=in.read())!=-1) System.out.print(temp+" "); System.out.println(); in.close(); } private static void printContent3(String src) throws IOException { in=new FileInputStream(src); byte[] buffer=new byte[DEFAULT_CAPACITY]; in.read(buffer); for(int i=0;buffer[i]!=0;i++) System.out.print(buffer[i]+" "); System.out.println(); in.close(); } private static void printContent4(String src) throws IOException { in=new FileInputStream(src); byte[] buffer=new byte[DEFAULT_CAPACITY]; /* in.read(buffer,2,5); for(int i=0;buffer[i]!=0;i++) System.out.print(buffer[i]+" "); //No output for(int i=0;i<DEFAULT_CAPACITY;i++) System.out.print(buffer[i]+" "); //Operation result: 0.072 101 108 108 111 0.000··· */ in.read(buffer,0,2); for(int i=0;buffer[i]!=0;i++) System.out.print(buffer[i]+" "); System.out.println(); in.close(); } public static void main(String[] args) throws IOException { printContent1("D:\\a.txt"); //a. The content of the txt file is: Hello Java printContent2("D:\\a.txt"); printContent3("D:\\a.txt"); printContent4("D:\\a.txt"); } }
2, OutputStream and FileOutputStream
OutputStream is an abstract class that is the parent of all classes representing byte output streams. The output stream receives the output bytes (writes) and sends them to a destination.
Common methods:
FileOutputStream writes data to files in bytes, suitable for all types of files.
Construction method:
The common methods of FileOutputStream and OutputStream are the same.
Test it:
package cn.zjb.test; import java.io.FileOutputStream; import java.io.IOException; /** * Application of test byte output stream * @author Zhang Jian Bo * */ public class TestOutputStream { private static FileOutputStream out; private static final String src="D:\\a.txt"; private static final byte[] buffer= {104,97,110,103}; // hang private static void writeInTxt1(String src) throws IOException { out=new FileOutputStream(src); out.write(122); //'z' out.close(); } private static void writeInTxt2(String src) throws IOException { out=new FileOutputStream(src,true); //Append at the end of source file out.write(buffer); out.close(); } public static void main(String[] args) throws IOException { writeInTxt1(src); //Run first writeInTxt2(src); } } }
3, Reader and FileReader
Reader is an abstract class, which is used to read character stream abstract class. The data unit is character. In Unicode, a character is equal to two bytes, that is, a 16 bit binary number.
Common methods:
The common methods of FileReader are the same as those in FileReader. Test the following:
package cn.zjb.test; import java.io.FileReader; import java.io.IOException; /** * Test character input stream * @author Zhang Jian Bo * */ public class TestFileReader { private static FileReader read; private static final String src="D:\\a.txt"; //zhang private static void readFile(String src) throws IOException { read=new FileReader(src); System.out.println(read.read()); read.close(); } public static void main(String[] args) throws IOException { readFile(src); //122 } }
4, Writer and FileWriter
Writer is an abstract class, which is used to write character stream abstract class. The data unit is character.
Common methods:
FileWriter construction method:
The common methods of FileWriter are the same as those in FileWriter. Test them as follows:
package cn.zjb.test; import java.io.FileWriter; import java.io.IOException; /** * Test character output stream * @author Zhang Jian Bo * */ public class TestFileWriter { private static FileWriter fw; private static final String src="D:\\a.txt"; private static final char[] buffer= {122,104,97,110,103,0x5f20}; //zhang Zhang private static void writeFile1(String src) throws IOException { fw=new FileWriter(src); //cover fw.write(122); fw.close(); } private static void writeFile2(String src) throws IOException { fw=new FileWriter(src,true); //Add to fw.write(buffer); fw.close(); } public static void main(String[] args) throws IOException { writeFile1(src); //Run first writeFile2(src); } }
I feel a little tired at the beginning of the class. My mentality