Java-io Xiao Kee

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.