Ashamed to say, work for more than a year, the IO is still not very understanding. Not only IO, but also network, and multi-threading. shame!!!
The following days, the first to engage in multi-threading, and then engage in IO, and then network, six months must be completed!
Well, today I finally understand the difference between the three write of OutputStream, here's a little bit to remember
1. Instructions in the documentation
write(byte[] b)
Writes b.length
bytes from the specified byte array to this file output stream.
write(byte[] b, int off, int len)
Writes len
bytes from the specified byte array starting at offset to this off
file output stream.
write(int b)
Writes the specified byte to this file output stream.
2. Understanding
①write (byte[] b) writes B.length bytes each time, even if the last byte array is not full, it will write B.lenth bytes.
eg
FileInputStream in =NewFileInputStream (NewFile ("C:/users/admin/desktop/js/node.txt")); byteB[] =New byte[50]; FileOutputStream out=NewFileOutputStream (NewFile ("C:/users/admin/desktop/js/node1.txt")); intLen = 0; System.out.println ("Total Length" +in.available ()); while(len = In.read (b))!=-1) {System.out.println (len); Out.write (b); } in.close (); Out.flush (); Out.close ();
Original file contents
Write the file
Console output
As can be seen, because the last read of the byte actually only 4, but the output of the file is a lot more (46 bytes should), it seems that the last read of the content was written to the
So, even if the last byte array is not full, it will write B.lenth bytes
② write(byte[] b, int off, int len)
Off, half is 0.
Len is the number of bytes to write this time.
Change the code above to
FileInputStream in =NewFileInputStream (NewFile ("C:/users/admin/desktop/js/node.txt")); byteB[] =New byte[50]; FileOutputStream out=NewFileOutputStream (NewFile ("C:/users/admin/desktop/js/node1.txt")); intLen = 0; System.out.println ("Total Length" +in.available ()); while(len = In.read (b))!=-1) {System.out.println (len); Out.write (b,0, Len); } in.close (); Out.flush (); Out.close ();
The difference lies in the Out.write method
In this case, the written file is no problem, the last time to read 4 bytes, write 4 bytes.
③write (int b)
Writes the specified byte to this output stream. The general contract-is-one byte is written to the write
output stream. The byte to being written is the eight low-order bits of the argument b
. The High-order bits of is b
ignored.
Don't know ...
Java-io Xiao Kee