How to select a stream object?

Source: Internet
Author: User

1. Clarify the source and purpose

Source: input stream

Inputstream

Reader

----------------

Objective: To output a stream

Outputstream

Writer

 

2. Whether the operated data is plain text

Yes: Batch stream

No: byte stream

 

3. When the system is clear, specify the specific object to use.

Distinguish between source devices:

1. Memory

2. Hard Disk

3. keyboard

Target device:

1. Memory

2. Hard Disk

3. Console

--------------------------------------------------

1. Copy an object

Source: because it is the source, the read stream is used. Inputstream Reader

Is it a text file operation?

Yes, then you can choose reader, so that the system is clear. ,

 

Next, identify the object in the system,

Device: hard disk, the last file.

In the reader system, the object that can operate files is filereader.

Filereader Fr = new filereader(“a.txt ");

Need to improve efficiency? Yes. Join the buffer zone in the reader system.

Bufferedreader bufr = new bufferedreader (FR );

 

 

 

Purpose: outputstream writer

Whether it is plain text

Yes. Writer

Device: hard disk, a file.

In the writer system, filewriter can operate files.

Filewriter fw = new filewriter(“ B .txt ");

Need to improve efficiency? Yes, just join the buffer in the writer System

Bufferedwriter bufw = new bufferedwriter (FW );

 

--------------------------------

Requirement: Save the data entered by the keyboard to a file

Source: inputstream Reader

1. Is it plain text? Yes, Reader

2. device? Keyboard. The corresponding object is system. In.

Didn't you select reader? Does system. In correspond to a byte stream?

In order to operate the text data of the keyboard conveniently, it is most convenient to convert the data into a text stream according to the string. Therefore, since reader is clearly defined, system. In is converted to reader.

Reader System Conversion stream is used. Inputstreamreader

Inputstreamreader ISR = new inputstreamreader (system. In );

Need to improve efficiency?

Bufferedreader bufr = new bufferedreader (ISR );

 

Purpose: outputstream writer

Is text stored? Yes, writer

Device? Hard disk, a file-> filewriter

Filewriter fw = new filewriter()a.txt ");

Need to improve efficiency? Yes

Bufferedwriter bufw = new bufferedwriter (FW );

--------------------------------------------------------------------

Expand to store the entered data according to the specified encoding.

Purpose:

Save text? Yes, writer

 

Device: hard disk, a file filewriter. However, filewriter uses the default encoding table. Encoding must be specified during storage. outputstreamwriter can specify encoding.

 

Outputstreamwriter OSW = new outputstreamwriter (New fileoutputstream(mongod.txt "," UTF-8 "));

Need efficiency?

Bufferedwriter bufw = new bufferedwriter (OSW );

So remember, when to use the conversion stream, the bridge between characters and bytes. Generally, the conversion stream is required when character encoding conversion is involved.

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.