Java Learning Share Input Output

Source: Internet
Author: User

IO (Input Output) Flow

IO streams are used to process data transfer between devices

The Java operation of the data is streamed through the way

The objects that Java uses to manipulate the flow are in the IO package

There are two types of flow by operation data: byte stream and character stream.

Flow is divided into: input stream, output stream.

IO Stream common base classes

Abstract base class for byte stream:

InputStream ,outputstream.

Abstract base class for character streams:

Reader , Writer.

Note: The subclass names derived from these four classes are the suffixes whose parent class name is the child class name.

( InputStream subclasses of FileInputStream. )

( the subclass of Reader is FileReader. )

IO Writing of the program

Import classes in IO packages

make IO Exception Handling

in finally , the convection is closed

Character Stream--Create file

Create a Stream object and set up a data store file

FileWriter FW = new FileWriter ("Test.txt");

Invokes the write method of the Stream object, writes the data to the stream

Fw.write ("text");

Closes the stream resource and empties the data in the stream into a file.

Fw.close ();

Full code

FileWriter FW = NULL;

try{

fw= new FileWriter ("Test.txt");

Fw.write ("text");

}

catch (IOException e) {

System.out.println (E.tostring ());

}

finally{

If (Fw!=null)

try{

Fw.close ();

}

catch (IOException e) {

System.out.println (E.tostring ());

}

}

Character stream--read file

Creates a stream object that loads a file that already exists into the stream.

FileReader FR = new FileReader ("Test.txt");

Creates an array of temporarily stored data.

char[] ch = new char[1024];

Invokes the Read method of the Stream object to read the data in the stream into the array.

Fr.read (CH);

Full code

FileReader FR = null;

try{

fr= new FileReader ("C:\\Test.txt");

Char[]buf = new char[1024];

intlen= 0;

while ((Len=fr.read (BUF))!=-1) {

System.out.println (NewString (Buf,0,len));

}

}

catch (IOException e) {

System.out.println ("Read-exception:" +e.tostring ());

}

finally{

if (fr!=null) {

try{

Fr.close ();

}

catch (IOException e) {

System.out.println ("Close-exception:" +e.tostring ());

}

}

}

Buffer of character stream

The presence of buffers increases the reading and writing efficiency of the data.

Corresponding class

BufferedWriter

BufferedReader

The buffer must be combined with a stream before it can be used.

The function of convection is enhanced on the basis of flow.

Decorative design pattern

The function of the original class is changed and enhanced.

The basic format of the adornment mode.

How does it differ from inheritance?

Understand the principle of bufferedreader.

BYTE stream

The basic operation is the same as the character stream class

But it can manipulate not only characters, but also other media files

Convert stream

Inputstreamreader,outputstreamwriter

The origin of the conversion stream

A bridge between a character stream and a byte stream

Facilitates operation between character stream and byte stream

Application of Conversion stream

When the data in a byte stream is a character, it is more efficient to turn into a character stream operation.

Standard input/output stream

fields in the System class: In,out.

They represent system-standard input and output devices.

The default input device is the keyboard, and the output device is the monitor.

the type of system.in is InputStream.

The type of System.out is a subclass of PrintStream, which is a subclass of OutputStream Filteroutputstream.

Example of a standard input and output stream

example : get keyboard input data, and then flow the data to the display, then the display is the destination.

through SetIn of the System class ,the SetOut method changes the default device.

System.setin (New FileInputStream ("1.txt"));// Change Source to file 1.txt.

System.setout (New FileOutputStream ("2.txt"));//Convert The purpose to file 2.txt

Because it is a byte stream processing is the text data, can be converted into a character stream, the operation is more convenient.

Bfferedreader BUFR =

Newbufferedreader (New InputStreamReader (system.in));

BufferedWriter BUFW =

Newbufferedwriter (New OutputStreamWriter (System.out));

Basic Application section of flow

The stream is used to process the data.

When working with data, be sure to first identify the data source, and the data destination (data sinks).

The data source can be a file, which can be a keyboard.

The data destination can be a file, monitor, or other device.

The flow is just helping the data to be transmitted and processing the transmitted data, such as filtering processing . conversion processing, and so on.

File class

Used to encapsulate a file or folder as an object

Facilitates the operation of File and folder property information.

the File object can be passed as a parameter to the constructor of the stream.

Understand common methods in the File class.

Recursive

function itself to call itself.

Note: Be sure to specify the end condition when recursion.

Application Scenarios:

When a feature is to be reused.

IO Other classes in the package

Randomaccessfile

Random access to the file, its own method of reading and writing.

by Skipbytes (INTX), Seek (int x) to achieve random access.

Pipe flow

PipedInputStream and PipedOutputStream

The input and output can be connected directly by using a combination of threads.

other classes in the IO package

Print Flow

PrintWriter and PrintStream

You can manipulate input streams and files directly.

Sequence Flow

Sequenceinputstream

Merges multiple streams.

Manipulating objects

ObjectInputStream and ObjectOutputStream

the object being manipulated needs to be implemented Serializable(labeled Interface );

Manipulating basic data types

DataInputStream and DataOutputStream

Manipulating byte arrays

Bytearrayinputstream and Bytearrayoutputstream

Manipulating character arrays

CharArrayReader and chararraywrite

Manipulating strings

StringReader and StringWriter

Character encoding

Character Stream appears in order to facilitate the manipulation of characters.

More importantly, the code conversion is added.

This is done through the subclass transformation stream.

InputStreamReader

OutputStreamWriter

A character set can be added when two objects are constructed.

recent 51cto+it 18 Palm Free Big Data course, interested and problem small partners can go Watch Xu Peicheng teacher Live class or video.


This article from the "11732319" blog, reproduced please contact the author!

Java learning to share input Output

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.