Java Preliminary I/O understanding

Source: Internet
Author: User

If we want to do a little bit of I/O, like copying files, I find a string of code on the Web:

1 ImportJava.io.*;2  Public classSS {3      Public Static voidMain (string[] args)throwsException {4File f =NewFile ("d:\\ large database file. mdf"); 5FileInputStream FIS =NewFileInputStream (f);6FileOutputStream fos =NewFileOutputStream ("e:\\" +f.getname ()); 7         intLength = 0;8         byte[] B =New byte[1024];9          while(length = Fis.read (b))! =-1)Ten         { OneFos.write (b, 0, length); A         } - fos.close (); - fis.close (); the     } -}

This code constructs a FileInputStream object with the file object, which is used as a read-in memory, and a FileOutputStream object that is used as output to the hard disk.

Every time I read 1024 bytes from the D disk to the memory, and then write to the E disk, it seems that there is no problem.

However, the disk I/O is turned on every time you read 1024 bytes, which can be very time consuming.

If you use Bufferedinputstream, it will be much better.

Bufferedinputstream in the internal maintenance of a buffer, every time we read is in the read buffer, only buffer is not large enough to go to the read disk. This can greatly reduce the number of I/O, because from the hard disk read to buffer at a time more than a byte, then read in the future can read from the buffer directly without the drive.

The NIO portion continues later

Java Preliminary I/O understanding

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.