Java I/O optimization tips

Source: Internet
Author: User

To improve I/O performance, it is primarily based on the following principles:

(1) Less access to the disk as little as possible

(2) Less access to the underlying operating system to access the underlying operating system as little as possible

(3) Less method calls to minimize method calls

(4) Can less individual processing byte characters as little as possible individual processing.

Tip 1: Use buffering to access the buffer as much as possible

For example, Character-based has BufferedReader, bufferedinputstream based on byte. Or you can use block-reading methods to improve performance.

Skill 2:inputstream is more efficient than reader, outputstream is more efficient than writer

Skill 3:byte is more efficient than char

A char holds the characters in 2 bytes, but a byte saves the data in only 1 bytes. In addition, byte can avoid Unicode conversions.

Tip 4: Buffer blocks are more efficient than buffer streams

Although buffer streams are more efficient than individual character accesses, they involve multiple method invocations, and buffer blocks are much more efficient than buffer streams.

Tip Close Stream in 5:finally

The various resources used in the program are closed in time, and finally blocks are the best places to close. Because finally blocks are always executed.

Tip 6: Release large object resources early and in time

such as database connection resources, and then closed in time, too long for the system overhead too much.

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.