Common Input/output stream case studies in java

Source: Internet
Author: User

Common Input/output stream case studies in java

Byte input stream:

1. FileInputStream

Purpose: Obtain input bytes from files in the file system. It is often used to read original byte streams such as images and sounds. to read the audio stream, consider using FileReader.

For more information about constructors and common methods, see the API documentation. The Chinese version of API is already available on the Internet. I am an E-literate and cannot afford to hurt.

Here is the most common method:

read(byte[] b, int off, int len)
Maximum number of input streamslenBytes of data is read into a byte array.

-> Off: the offset in the B-byte array.

TIPS: array offset, for example, a [1, 2, 3, 4, 5] array. By default, the first array should point to a [0]. If the offset is 2, it will point to a [1].

Case:

Import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; public class Test {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stubtry {int B; // create the input stream FileInputStream readfile = new FileInputStream ("E: \ test.txt"); byte buffer [] = new byte [2500]; // create a byte array // read bytes from the input stream and store them in the buffer array. The maximum length is 2500 bytes. The returned value B is the actual read length B = readfile. read (buffer, 1, 2000); // 1 is the starting offset of the buffer array String str = new String (buffer, 0, B, "Default"); System. out. println (str); System. out. println (B); readfile. close ();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

There are still many frequently used stream operations to be continued, which will be supplemented later. it's very late. it's time to go to bed. I can do it every day !!


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.