IO stream for Java

Source: Internet
Author: User

There are many applications for streaming, one of which is socekt (including TCP and UDP) and requires a combination of learning

First, the basic concept

1. What is a stream?

A pipeline for data transfer between programs and devices, which has a number of buttons that enable different functions with different buttons.

2. What is the classification of streams?

(1) According to the direction of flow: input stream, output stream

(2) According to the transmitted data type: byte stream, character stream

(3) Depending on the function: Raw Stream (node stream), wrap flow (processing flow)

3. Differences in some streams

(1) Four basic flows: Inputstream,outputstream,reader,writer is the original flow

Note: All output streams have the Flush method, and the input stream does not

(2) file stream: Fileinputstream,fileoutputstream,filereader,filewriter is the package stream.

Where the byte stream can be used to complete the copying of files in all formats, but the character stream only completes the text file copy;

In addition, the byte stream does not use the cache when processing the input and output, but the character stream will use the cache (the main difference);

When the content of the text file is entered into the computer and displayed, the character stream can be, but the byte stream is read in bytes, the input to the display with character output will produce garbled!

(3) Buffer stream: Bufferinputstream,bufferoutputsream,bufferreader,bufferwriter is the package flow

Features: Can reduce the number of accesses to the IO, thereby protecting the computer hard disk

Bufferinputstream and Bufferoutputstream can replicate large volumes of files, which is much faster than using InputStream and OutputStream alone.

Note: Bis,read (buf,0,1024) reads the data from the file associated with the BIS, writes it to the BIS default buffer, and writes the contents of the buffer to the BUF array.

Writes up to 1024 bytes at a time, returns the number of bytes actually written to the BUF array, and, if it ends, can no longer write to the BUF array, returning 1

There is a very important and useful method ReadLine in the BufferedReader stream, which can improve the reading and writing speed.

Also note that the ReadLine () method stops when it encounters a newline, but automatically discards the read newline character, so it is generally necessary to newline ().

(4) Data flow: Datainputstream,dataoutputstream,datareader,datawriter is the package flow

A feature is a method that contains data that can read or write arbitrary basic types, such as Dos.writelong (n) and Readboolean readInt ReadChar readfloat readUTF actually reads the String class

Also note that Writelong (Long N) in the DataOutputStream stream writes the binary code of the n variable in memory to the device associated with the stream

(5) Conversion flow: Inputstreamreader,outputstreamwriter

Example: Displaying the characters entered by the keyboard

(6) Print Stream: Printstream,printwriter

Features: Formatted output for different data types (outputs a data in its string format) (differential data flow)

PrintStream can only encapsulate the byte stream of the OutputStream class, and PrintWriter can encapsulate not only outputstream but also writer. PrintWriter in println has cross-platform

Two of these usages: System.setout (psout);//

System.seterr (pserror);//Output error information to the associated device display

(7) An object stream, also known as a serialization of objects

In particular, the object serialization here is to convert an object into a byte stream directly, and then write the byte stream directly to the local hard disk or network.

Must implement the Serializable interface, is an identity interface, there is no method

Methods that are modified with transient are not serialized

IO stream for Java

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.