The (input/output) stream in Java is nothing more than character-based stream, byte-based stream, and byte-oriented Stream Conversion.
Character-oriented stream. (Difficult to understand ?)
Byte-oriented stream ------ inputstream/outputstream
Inputstream and outputstream are two abstact classes. For byte-oriented stream, these two chicken ribs are extended (base class pai_^ );
-- Inputstream
Bytearrayinputstream -- uses a buffer in the memory as an inputstream. Construct --- bytearrayinputstream (byte []) to create a new byte array input stream, which reads data from the specified byte array.
--- Bytearrayinputstream (byte [], Int, INT) creates a new byte array input stream, which reads data from the specified byte array.
--- Mark: the byte array is not copied. Stringbufferinputstream -- takes a String object as inputstream.
Note: The stringbufferinputstream method is not recommended. This class cannot correctly convert characters into bytes.
Similar to JDK 1.1, the best way to create a stream from a string is to use the stringreader class. Construct --- stringbufferinputstream (string) creates an input stream string to read data. Fileinputstream -- uses a file as the inputstream to read the file. Construct --- fileinputstream (File) creates an input file stream to read data from the specified file object.
--- Fileinputstream (filedescriptor) creates an input file stream and reads data from the specified file descriptor.
--- Fileinputstream (string) creates an input file stream to read data from a file with the specified name. Method ---- read () reads one byte of data from the current input stream.
Read (byte []) reads B. length bytes of data in the current input stream to a byte array.
Read (byte [], Int, INT) reads len bytes of data in the input stream into a byte array. Pipedinputstream: implements the concept of pipe. It is mainly used in the process. The pipeline input stream refers to the receiving end of a communication pipeline.
One thread sends data through the output stream of the pipeline, while the other thread reads data through the input stream of the pipeline,
In this way, communication between two threads can be realized. Pipedinputstream () creates a pipeline input stream, which is not connected to a pipeline output stream.
Pipedinputstream (pipedoutputstream) creates a pipeline input stream, which is connected to a pipeline output stream. Sequenceinputstream: combines multiple inputstreams into one inputstream. The "sequential input stream" class allows applications to merge several input streams consecutively,
And make them appear like a single input stream. Each input stream is read until it reaches the end of the stream.
Then, the "sequence input stream" class closes the stream and automatically switches to the next input stream.
Sequenceinputstream (enumeration) creates a new sequence input stream and initializes it with the enumerated value of the specified input stream.
Sequenceinputstream (inputstream, inputstream) creates a new sequence input stream, initialized to first read the input stream S1, and then read the input stream S2. -- Outputsteambytearrayoutputstream: stores information in a buffer zone in the memory. This class implements an output stream that writes data in byte arrays.
It automatically expands when data is written to the buffer zone. You can use tobytearray () and tostring () to retrieve data. Construct --- bytearrayoutputstream () creates a new byte array output stream.
--- Bytearrayoutputstream () creates a new byte array output stream.
--- Bytearrayoutputstream (INT) creates a new byte array output stream with the buffer capacity of the specified size bytes.
Tostring (string) converts the buffer content to a string based on the specified character encoding and converts bytes to characters.
Write (byte [], Int, INT) writes the len bytes starting from offset off in the specified byte array to the output stream of the byte array.
Write (INT) writes the specified byte to the output stream of the byte array.
Writeto (outputstream) uses out. Write (BUF, 0, count) to call the write method of the output stream to write all the content of the output stream of the byte array to the specified output stream parameter. Fileoutputstream: An output stream that outputs data to a file or filedescriptor. Fileoutputstream (File) creates a file output stream and outputs data to the specified file object.
Fileoutputstream (filedescriptor) creates a file output stream and outputs data to the specified file descriptor.
Fileoutputstream (string) creates a file output stream and outputs data to the file with the specified name.
Fileoutputstream (string, Boolean) creates an output file by specifying the system file name. Pipedoutputstream: the output stream of a media transcoding queue. A thread sends data through an output stream in a pipeline,
The other thread reads data through the input stream of the pipeline, which enables communication between two threads. Pipedoutputstream () creates an output stream of the pipeline, which is not connected to an input stream of the pipeline.
Pipedoutputstream (pipedinputstream) creates an output stream of the pipeline, which is connected to an input stream of the pipeline. Character-oriented stream Reader/writer is a Unicode-oriented stream that reads or writes information from or to the stream in units of Unicode characters.
Reader/writer is a class of abstact
Unicode-oriented stream includes the following types: -- reader1) chararrayreader: corresponds to bytearrayinputstream
Chararrayreader (char []) creates a chararrayreader with a specified character array.
Chararrayreader (char [], Int, INT) creates a chararrayreader with a specified character array. 2) stringreader: corresponds to stringbufferinputstream
Stringreader (string) creates a new string reader.
3) filereader: corresponds to fileinputstream 4) pipedreader: corresponds to pipedinputstream -- writer1) chararraywrite: corresponds to bytearrayoutputstream
2) stringwrite: no corresponding byte-oriented stream
3) filewrite: corresponds to fileoutputstream
4) pipedwrite: the conversion between two non-existing stream types corresponding to pipedoutputstream
Inputstreamreader and outputstreamreader: convert a byte-oriented stream into a character-oriented stream.
An inputstreamreader class is a bridge between byte streams and bytes streams: It reads bytes and converts them to bytes streams based on the specified encoding method.
The encoding method may be specified by the name or the default encoding method acceptable to the platform. Each call to one of the read () Methods of inputstreamreader may prompt reading one or more bytes from the basic byte input stream.
To achieve higher efficiency, we consider using bufferedreader to encapsulate inputstreamreader,
Bufferedreader in = new bufferedreader (New inputstreamreader (system. In); inputstreamreader (inputstream) uses the default character encoding method to create an inputstreamreader.
Inputstreamreader (inputstream, string) uses a named character encoding method to create an inputstreamreader. Outputstreamwriter writes multiple characters to an output stream and converts multiple characters to bytes based on the specified character encoding.
Each outputstreamwriter combines its own chartobyteconverter, which serves as a bridge from the bytes stream to the byte stream. For filterinputstream and randomaccessfile, see the example.
Objectinputstream and objectoutputstream can be found in another blog. General Usage principles of Java IO: 1. Classification by data source (whereabouts:
1. File: fileinputstream, fileoutputstream, filereader, filewriter
2. byte []: bytearrayinputstream, bytearrayoutputstream
3. Char []: chararrayreader, chararraywriter
4. String: stringbufferinputstream, stringreader, stringwriter
5. network data streams: inputstream, outputstream, reader, and writer. 2. Output score based on whether to format:
1. to format the output: printstream, printwriter 3. Whether to buffer the output:
1. Buffer: bufferedinputstream, bufferedoutputstream, bufferedreader, and bufferedwriter. 4. Data Format:
1. binary format (as long as it cannot be determined that it is plain text): inputstream, outputstream and all its sub-classes with stream termination
2. plain text format (including English and Chinese characters or other encoding methods). Reader, writer, and all sub-classes with reader and writer 5. Input and Output:
1. Input: reader, a subclass of the inputstream type
2. Output: writer, a subclass of the outputstream type 6. Special requirements:
1. Conversion class from stream to reader: inputstreamreader and outputstreamwriter
2. Object input and output: objectinputstream and objectoutputstream
3. inter-process communication: pipeinputstream, pipeoutputstream, pipereader, and pipewriter
4. Merge input: sequenceinputstream
5. Special requirements: pushbackinputstream, pushbackreader, linenumberinputstream, and linenumberreader determine which class to use and the General Principles for constructing processes are as follows (special requirements are not considered ):
First, consider what is the most primitive data format: Principle 4
Second, whether input or output: Principle 5
Third, whether stream conversion is required: Principle 6
Fourth, what is the data source (destination): Principle 1
Fifth, whether to buffer: Principle 3 (Note: it must be noted that Readline () is defined, and there are more special input or output methods than read and write)
Sixth, whether to format the output: Principle 2 Article 2:
The first is Java Io. This is really troublesome. I/O class libraries often use the abstraction of "stream. The so-called "stream" is an object that can generate or accept data and represents the source and target of the data. The stream hides the specific operations inside the I/O device. As shown in the JDK document, Java I/O class libraries are divided into two parts: input and output. All the derived classes of inputstream and reader have a basic inherited read () method that can read a single or byte array. Similarly, all the derived classes of outputstream and writer have a basic write () method that can write a single or byte array. But in general, you will not use these methods; they are used for other classes -- and the latter will provide some more practical interfaces. Therefore, you rarely encounter a situation where only one class can be used to create a stream. In fact, you have to fold multiple objects to obtain the required functions. The main reason why Java's stream class library is so confusing is that "you must use multiple objects to create a stream ".
Java Io class structure:
The Root Interface is inputstream/outputstream. The IO classes acting as data sources include fileinputstream/fileoutputstream, Bytes/bytearrayoutputstream, etc. The IO classes acting as decorative functions include bufferedinputstream/inputs, and datainputstream,
They all inherit from the decoration interface filterinputstream/filteroutputstream.
When I/O is used, create a data source I/O first, and then create a decoration class I/O Based on the required functions. The constructor parameter is the created data source I/O.
Taking the creation of a buffer file input stream as an example, assume that the file "C:/log.txt" needs to be read from the disk ":
// Create a fileinputstream:
Fileinputstream fileinput = new fileinputstream ("C: // log.txt ");
// Create a bufferedinputstream:
Bufferedinputstream bufferedinput = new bufferedinputstream (fileinput );
// The obtained bufferedinput is a buffer file input stream.
Or, you can simply enter the following:
Inputstream input = new bufferedinputstream (New fileinputstream ("C: // log.txt "));
// The input obtained now is a buffer file input stream.
Differences between Java. Io. Reader and Java. Io. inputstream
Java. Io. Reader and Java. Io. inputstream constitute JAVA input classes. Reader is used to read 16 characters, that is, Unicode-encoded characters. inputstream is used to read ASCII characters and binary data.
In Java, different types of Reader Input streams correspond to different data sources:
Filereader is used for input from a file;
Chararrayreader is used to input character arrays from the program;
Stringreader is used to input strings from the program;
Pipedreader is used to read data written to the pipeline from pipedwriter in another thread.
Different types of inputstream input streams correspond to different data sources: fileinputstream, bytearrayinputstream, stringbufferinputstream, and pipedinputstream. In addition, there are two inputstream input streams that do not have the corresponding Reader Type:
Socket is used for sockets;
Urlconnection is used for URL Connection.
These two classes use getinputstream () to read data.
Correspondingly, there are similar differences between Java. Io. Writer and Java. Io. outputstream.
1. Java technology supports two data types of streams
Inputstream and outputstream: byte stream. Other byte streams are subclasses of inputstream or outputstream.
Reader and Writer: Internal stream. Other streams are subclasses of reader or writer.
2. node stream
Java 2 SDK has three basic types of nodes: file, memory, and pipe ).
3. Process Flow
Process streams are placed above other streams to complete sorting, transformation, and other operations. A process stream is also called a filter stream.
When you need to change the original data of the input stream, you can connect a filtered input stream to an original input stream.
Use a filter stream to convert the raw data into the format you need.
4. Basic byte streams
4.1. fileinputstream and fileoutputstream
These two node streams are used to manipulate disk files. Constructors of these classes allow you to specify the files they are connected.
To construct a fileinputstream, the associated file must exist and be readable.
If you want to construct a fileoutputstream and the output file already exists, it will be overwritten.
Fileinputstream infile = new fileinputstream ("myfile. dat ");
Fileoutputstream OUTFILE = new fileoutputstream ("results. dat ");
4.1 bufferinputstream and bufferoutputstream
These are filter streams, which can improve the efficiency of I/O operations.
4.3. pipedinputstream and pipedoutputstream
The pipeline stream is used for communication between threads. The pipedinputstream object of one thread reads the input from the pipedoutputstream object of another thread.
To make the pipeline stream useful, you must have an input party and an output party.
4.4. datainputstream and dataoutputstream
These filters read and write Basic Java classes through streams
5. Basic streams
The figure illustrates the architecture of reader and writer internal streams.
5.1. inputstreamreader and outputstreamwriter
This interface is used to convert byte streams to byte streams.
When you construct an inputstreamreader or outputstreamwriter, the conversion rules define the conversion between the 16-bit Unicode and the specific representation of other platforms.
Inputstreamreader reads bytes from a data source and automatically converts them to unicode characters.
If you specifically declare that inputstreamreade will replace byte streams with other types of bytes streams.
Outputstreamwriter writes the Unicode encoding of characters to the output stream. If you are not using Unicode characters, outputstreamwriter converts your character encoding to unicode encoding.
5.2. Buffer readers and authors
Because the conversion between different formats is similar to other I/O operations, the maximum efficiency of processing large data blocks is reached.
It is a good idea to link a bufferedreader and bufferedwriter at the end of inputstreamreader and outputstreamwriter.
Remember to use the flush () method for bufferedwriter.
5.3 use other characters for conversion
If you need to read the input from a non-local character code (for example, from a network connection to a different type of machine,
You can use explicit character encoding to construct IR = new inputstreamreader (system. In, "8859_1") Like the following program ″);
Note: If you read characters through a network connection, you should use this form.
Otherwise, your program will always try to convert the characters read as a local representation, which is not always correct. ISO 8859-1 is the Latin-1 encoding mode mapped to ASCII.
6. Object serialization
The Java. Io. serializable interface supports storing a Java technical object in a stream.
Storing an object in a type of permanent storage is called "persistence ".
If an object can be stored on a disk or tape, or can be sent to another machine and stored in a storage or disk, the object is called a retainable object.
The Java. Io. serializable interface does not have any method. It serves only as a "mark" to indicate that the classes that implement this interface can be serialized.
Objects that do not implement the serializable interface in the class cannot be kept.
// Append an object:
// The second parameter in filewriter () indicates whether to Append content to the file.
Printwriter out = new printwriter (New filewriter (logfilename, true), true );
The most common classes for reading and writing files in Java are fileinputstream/fileoutputstream and filereader/filewriter.
Fileinputstream and fileoutputstream are based on byte streams and are often used to read and write binary files.
We recommend that you use character-based filereader and filewriter to read and write character files, eliminating the need for conversion between bytes and characters.
However, the constructors of these two classes use the system encoding method by default. If the file content is inconsistent with the system encoding method, garbled characters may occur.
In this case, we recommend that you use the parent class of filereader and filewriter: inputstreamreader/outputstreamwriter,
They are also character-based, but the constructor can specify the encoding types: inputstreamreader (inputstream in, charset CS) and outputstreamwriter (outputstream out, charset CS ). // Read/write file encoding:
Inputstreamreader r = new inputstreamreader (New fileinputstream (filename), "UTF-8 ″);
Outputstreamwriter out = new outputstreamwriter (New fileoutputstream (filename), "UTF-8"); this article from the csdn blog, reprint please indicate the source: http://blog.csdn.net/thewk/archive/2010/01/22/5224093.aspx
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