IO Stream Classification:
A: Flow Direction
Input stream Read Data
Output stream writes out data
B: Data type
BYTE stream
BYTE input stream
BYTE output stream
Character Stream
Character input stream
Character output stream
Attention:
A: If we do not specify according to what points, by default according to the data type points.
B: It is recommended to use a byte stream unless the file is opened in a Windows-brought notepad that we can read.
Structure
FileOutputStream Write Data
FileOutputStream fos = new FileOutputStream ("F.txt")
Fos.write ("Hello". GetBytes ());//Note that the write is a byte array
fos.close//This step must be, release resources, you can be automatically recovered by the system
Next about writing the data, using the Write method to write each write the data is re-written, sometimes we need to append the write, at this time can use, fileoutputstream fos = new FileOutputStream ("F.txt", True )
FileInputStream reading data
FileInputStream fis = new FileInputStream ("Fos.txt");
Mode 1 This way is not recognized in Chinese
int by = 0;
while ((By=fis.read ())!=-1) {
System.out.print ((char) by);
}
Mode 2 This way is more efficient than the previous one, as it is one time to read multiple data in the buffer
byte[] bys = new byte[1024];
int len = 0;
while ((Len=fis.read (bys))!=-1) {
System.out.print (New String (Bys,0,len));
}
With the comparison of the two methods, we can see that the input and output of the buffer is more efficient, so there is a byte class of the buffer in Java.
FileOutputStream fos = new FileOutputStream ("F.txt");
Bufferedoutputstream fos = new Bufferoutputstream (FOS);
Both can be combined
Bufferedoutputstream fos = new Bufferoutputstream (New FileOutputStream ("F.txt"));
The other methods used are the same.
Java Learning Day 21st (use of IO stream)