20155335 Eucun "Java Programming" 6th Week summary

Source: Internet
Author: User
Tags java se

20155335 Java Programming 6th Week of study summary

# # Textbook Learning Content Summary

First, we need to understand the relationship between input and output, which I think is different from the input and output in C, and we first understand that Java is using streaming (stream) to abstract the concepts of input and output, as well as the InputStream and OutStream inheritance schemas.

If the data is output from the source, the input stream can be used, and if the data is written to the destination, the output stream can be read out, and in Java the input stream represents the Java.io.InputStream object. The output stream represents the object when the Java.io.OutputStream instance, what we do is to try to get InputStream and OutputStream instances.

To solve the problem is the source and destination are not aware of the situation how to write a program, you can design a general dump () method,

?

The Dump method accepts InputStream and OutStream instances, representing the source of the data, as well as the output data and destination, if an error occurs in connection with InputStream and OutStream Throws a Java.io.IOExprction exception, which is not handled here specifically, but is declared throws on the dump method, handled by the client that calls the dump () method.

Without using the InputStream and OutStream methods, you must close the stream with the close () method, which operates the Java.io.Closeable interface, whose parent interface is the Java.lang.AutoClose interface. When reading data, you can use the write () method to specify the byte array to write out, the initial index, and the data length.

The source of the dump method also relies on InputStream and OutStream, reading a document into and saving it as another document, so

?

If you read a webpage from an HTTP server and save it as a document, you can use the design dump method, for example

?

In order to understand the concept of stream abstraction data source and destination, it is necessary to clarify the inheritance architecture of InputStream and OutStream, and make a simple exposition of it. Namely, Bufferedinputstream and DataInputStream together constitute Filterinputstream,filterinputstream and FileInputStream, ObjectInputStream and Bytearrayinputstream together constitute InputStream.

This can be explained by the common class inheritance schema for OutputStream.

It consists of FileOutputStream and Bytearrayoutputstream and ObjectOutputStream and Fiteroutputstream, The Fiteroutputstream is composed of Bufferedoutputstream, DataOutputStream and PrintStream.

The system.in and System.out represent standard inputs and standard outputs, respectively, and correspond to the input and output of text patterns for personal computers. For system.in, it is less straightforward to manipulate the InputStream method, but rather as the Java.util.Scanner packaging system.in used in the previous section, if the operation scanner the related type method, the Scaneer type will operate on my behalf system.in get the data.

To reassign the standard output source, you can use the system's Setin () method to specify the InputStream instance, and the following example intentionally specifies that the standard input is fileinputstream to read and specify the document and display it in text mode.

?

You can use the system's SetOut () method to specify the PrintStream instance to output the results to a specified destination, such as intentionally outputting the standard to a document:

?

PrintStream Accept Innputstream instances, in this example PrintStream packaging Fileoutstream I operate printstream related methods, in the program, PrintStream will operate filstream for me.

FileInputStream is a subclass of InputStream, you can specify the file name creation instance, the document opens, can be used to read the data. FileOutputStream mainly operates the InputStream read () abstract method, which can manipulate the data from the document, FileOutputStream mainly manipulate the output of the write () abstract method, is the data available to the document, Front of the Io.dump

Method has been demonstrated by the method.

Bytearrayinputsteam and Bytearrayoutputstream

Bytearrayinputstream is a subclass of InputStream, you can specify a byte array to create an instance, once created, the data source can be read, Bytearrayoutput is a subclass of OutputStream, You can specify a byte array to create an instance, once the byte array is created as the destination write data.

Bytearrayinputstream mainly operates the read () abstract method of InputStream, which is the ability to read data from a byte array. The previous io.dump () method has been demonstrated.

For a streaming decorator, you can use the Packager class if you want to do processing of input/output data.

Some subclasses of InputStream and OutStream have the function of Packager, when the subclass is created, can accept InputStream, outputstream instance, I operate PrintStream's print (), printin (), etc. PrintStream automatically converts array data, using packaged outputstream for output.

Because some classes themselves do not change the behavior of InputStream and OutStream, only to get the data after doing some processing, or output after doing some processing, to outputstream for the real output, so called them as adorners.

Commonly used stream decoration class, the most typical is the Bufferdinputstream and Bufferedoutputstream instances, each read () will require reading the hard disk, write will require writing to the hard disk.

Rewrite the previous io.dump to buffered. The Io.dump method is as follows

?

For DataInputStream and DataOutputStream, which provide methods for reading and writing Java basic data types, such as decorating InputStream, these methods can be converted automatically.

The following member class can call the save () stored member instance itself data, the file name is member's membership number, call Member.load () specify the membership number, you can read the document member data, encapsulated as member after the instance returned.

?

Character Processing classes

The inheritance architecture of reader and writer

For reading character data, Java SE provides the Java.io.reader class, which abstracts the source of the character data read-in, and writes to the character data, providing the Java.io.Writer class, which abstracts the destination of the data written out.

If you want to read character data from a source, or write character data to a destination, you can use the following Charutil.dump () method.

The Dump method accepts reader and writer instances, representing the source of the read data, and the witness of the output data, which throws a Ioexpection exception if there is an error in the related operation of reader and writer, where The throws is declared on the dump () method, which is handled by the client that called the dump () method.

20155335 Eucun "Java Programming" 6th Week summary

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.