Java data stream Overview

Source: Internet
Author: User

JAVA data stream Overview

Considering the diversity of data sources, in order to effectively perform data input and output operations, data transmission between different data sources and programs is represented as "Stream ), to achieve relatively unified and simple input/Output (Inout/Output, I/O) operations.

Next let's take a look at the java data stream Overview:

Stream overview stream classification:

• Data Flow

-Input stream: Only bytes of data can be read from it, but data cannot be written to it.

-Output stream: Only bytes of data can be written to it, but data cannot be read from it.

• Data Types processed by stream

-Byte stream: used to process byte data.

-Character stream: used to process UNICODE character data

• Source processed by stream

-Node stream: the stream that reads/writes data from/to a specific IO Device.

-Stream processing: Connection and encapsulation of existing streams

Filter stream

Abstract inputstream and outputstream classes allow us to read and write strings and numbers. For this purpose, more sub-classes are required. For example, dateinputstream and dataoutputstream allow us to read and write all basic java types.

The file stream class is similar to the abstract inputstream and outputstream classes. These classes only support byte-level read/write operations. In other words, you can only read character and byte arrays from the fin object. Byteb = fin. Read (), they do not provide a special numeric type, so datainputstream cannot get a number from a file.

Solution: Java divides the responsibilities of a stream. Some streams (fileinputstream) are responsible for reading byte data from files or other special places. Other streams

(Datainputstream, printwriter) is responsible for "assembling" bytes into more useful data types. These two streams must be integrated into the so-called "filteredstreams" by hand to the builder of another stream.

Data Stream

L datainputstream

L dataoutputstream

For the class structure and members, see the documentation.

// Write and read the name and salary in the file

ImportJava. Io .*;

Public classT1 {

Public static voidMain (string [] ARGs ){

String [] Name = {"Tom", "Jack", "Mary "};

Double[] Salary = {300,400,120 0 };

Try{

Fileoutputstream Fos =
New
Fileoutputstream ("Res/salary.txt ");

Dataoutputstream dos =
New
Dataoutputstream (FOS );

For(IntI = 0; I <name. length; I ++ ){

Dos. writeUTF (name [I]);

Dos. writeDouble (salary [I]);

}

Dos. close ();

Fos. close ();

///////////////////////

FileInputStream fin =
New
FileInputStream ("res/salary.txt ");

DataInputStream dis =
New
DataInputStream (fin );

For(IntI = 0; I <name. length; I ++ ){

String n = dis. readUTF ();

DoubleS = dis. readDouble ();

System.Out. Println (n + ":" + s );

}

Dis. close ();

Fin. close ();

}Catch(Exception e ){

E. printStackTrace ();

}

}

}

Byte buffer stream

L BufferedInputStream

L BufferedOutputStrem

For the class structure and members, see the documentation.

By default, the stream is not buffered. That is, each read operation requires the operating system to provide one byte. Through bufferedinputstream and

Bufferedoutputstream filters and layers the stream builder to implement buffering.

1. constructor:

Bufferedinputstream (inputstream in)

Bufferedinputstream (inputstream in, intsize) // size: buffer size

2. Code Demonstration:

Bufferedinputstream Bis = newbufferedinputstream (system. In );

Bufferedinputstream Bis = newbufferedinputstream (system. In, 100 );

// Use a buffer byte stream to read and write files

ImportJava. Io .*;

Public classBufferediotest {

Public static voidMain (string [] ARGs ){

Try{

Fileoutputstream Fos =
New
Fileoutputstream ("Res/new.txt ");

Bufferedoutputstream Bos =
NewBufferedoutputstream (FOS );

Dataoutputstream dos =
New
Dataoutputstream (BOS );

Dos. writeutf ("GoodMorning! ");

Dos. writeint (250 );

Dos. Close ();

Bos. Close ();

FOS. Close ();

Fileinputstream FCM =
New
Fileinputstream ("Res/new.txt ");

Bufferedinputstream Bis =
NewBufferedinputstream (FCM );

Datainputstream Dis =
New
Datainputstream (bis );

System.Out. Println (DIS. readutf () + "\ n" + dis. readint ());

Dis. Close ();

Bis. Close ();

FCM. close ();

}Catch(Exception e ){

E. printStackTrace ();

}

}

}

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.