[JAVA & #183; elementary]: 20. IO stream technology
Principle
In Java programs, data input/output operations are performed in the stream mode. J2SDK provides various "stream" classes to obtain different types of data; input or output data through standard methods in the program.
Concept
Data source provides the original media of raw data. Common: databases, files, other programs, memory, network connections, and I/O devices.
The data source is like a water tank, and the stream is like the water flow in the water pipe. The program is our end user. Stream is an abstract and dynamic concept and a series of continuous and dynamic data sets.
Category
? Stream direction:
Input stream: data source to program (read in InputStream and Reader)
Output stream: Write the program to the destination (OutPutStream and Writer)
? Data Processing Unit:
Byte stream: reads data by byte (InputStream, OutputStream)
Bytes stream: reads data by character (Reader, Writer)
? Different functions:
Node stream: you can read and write data directly from the data source or destination.
Stream processing: the stream that processes the stream instead of directly connecting to the data source or destination. Improve program performance by processing other streams.
? Relationship between node stream and processing stream:
The node stream is on the front line of io operations, and all operations must be performed by them. The processing stream can process other streams (improving efficiency or operational flexibility ).
System
Byte stream
? ByteArrayInutStream and ByteArrayOutputStream
? Data source or destination: byte array
? BufferedInputStream and BufferedOutputStream are stream processing. They use an internal cache array to improve the efficiency of the operation stream.
? DataInputStream and DataOutputStream provide methods to access all basic Java data types (such as int and double.
Demo
Package com. bjsxt. wsl; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; import java. io. inputStream; public class HelloIn {public static void main (String [] args) throws IOException {// get the file path InputStream inputStream = new FileInputStream ("D :\\ hello.txt "); // generally, StringBuilder builder = new StringBuilder (); byte [] bs = new byte [1024]; int Num = 0; // start reading data while (num = inputStream. read (bs ))! =-1) {builder. append (new String (bs);} System. out. println (builder );}
Ghost stream
? Reader:
The abstract class used to read the livestream. The data unit is a character.
? Writer:
The abstract class that writes streams.
? BufferReader and BufferWriter
This pair is generally used to read text!
Stream Processing
Object serialization and deserialization:
ObjectInputStream and ObjectOutputStream. The data source or destination is: object!
Print stream:
Output operations of PrintWriter and PrintStream do not throw an exception. You can obtain error information by checking the error status.
System. out is the print stream.
InputStreamReader and OutputStreamWrite are processing streams: used to convert bytes into bytes streams.
File class
Abstract representation of file and directory path names. A File object can represent a File or directory, but not exactly. Creating a File object does not affect the File system.
Business Philosophy
The use of IO stream technology makes our work more smooth. It is easy to understand from the perspective of physical storage or logical thinking.
Here is just a brief introduction to the I/O stream technology. Many details need to be put into practice in the project to continue to get a new understanding.
Due to frequent consideration of exceptions, the problem may be considered comprehensively, which greatly improves our business capabilities!