Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the standard data streams of Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are Java's standard data streams". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "what are Java's standard data streams" together!

Java I/O API is one of the most important APIs in Java. Java I/O provides many APIs for reading and writing files, memory, and sockets.

Stream concepts in Java:

A stream is an ordered byte data object. Stream is divided into input stream and output stream. The input stream reads byte data from external resources (files, memory, sockets, etc.) into Java objects; the output stream writes Java objects (byte data, etc.) to external resources.

All Java I/O can be classified into two categories:

1, byte data input output I/O

2, Text column data input output I/O

All byte data input and output I/O are inherited from the java.io.InputStream and java.io.OutputStream interfaces.

Byte data input stream and its derivatives:

java.io.InputStream +--java.io.ByteArrayInputStream +--java.io.BufferedInputStream +--java.io.DataInputStream +--java.io.FileInputStream +--java.io.FilterInputStream +--java.io.ObjectInputStream +--java.io.PipedInputStream +--java.io.PushbackInputStream +--java.io.SequenceInputStream +--java.io.StringBufferInputStream

Byte data output streams and their derivatives:

java.io.OutputStream +--java.io.BufferedOutputStream +--java.io.ByteArrayOutputStream +--java.io.DataOutputStream +--java.io.FileOutputStream +--java.io.FilterOutputStream +--java.io.ObjectOutputStream +--java.io.PipedOutputStream

All text column data input and output I/O are inherited from java.io.Reader and java.io.Writer interfaces.

Text column data input stream and its derivatives:

java.io.Reader +--java.io.BufferedReader +--java.io.CharArrayReader +--java.io.FileReader +--java.io.FilterReader +--java.io.InputStreamReader +--java.io.LineNumberReader +--java.io.PipedReader +--java.io.PushbackReader +--java.io.StringReader

Text column data output stream and its derivatives:

java.io.Writer +--java.io.BufferedWriter +--java.io.CharArrayWriter +--java.io.FilterWriter +--java.io.OutputStreamWriter +--java.io.FileWriter +--java.io.PipedWriter +--java.io.PrintWriter +--java.io.StringWriter

The relationship between them (java.io) can be represented by the following diagram:

Java I/O operations prior to JDK 1.4 were concentrated in java.io, a stream-based blocking API.

The New I/O(NIO)API was introduced in JDK 1.4. This API is included in java.nio.* inside. NIO, sometimes referred to as nonblocking I/O, is buffer-based and provides non-blocking IO operations.

NIO main package:

java.nio

Buffer and its data type-dependent subclasses are defined.

java.nio.channels

Channel interfaces for I/O processing such as high-speed file processing/socket communication processing and implementation classes for these interfaces in file systems and network communications are defined. At the same time, you can provide methods for non-blocking I/O operations through the Selector class. This package is the core package of the NIO API.

java.nio.charset

Character encoding and decoding processing classes are defined.

Hierarchy of NIO interfaces/classes:

java.nio.ByteBuffer java.nio.channels.Channel +--java.nio.channels.ServerSocketChannel +--java.nio.channels.ReadableByteChannel +--java.nio.channels.ScatteringByteChannel +--java.nio.channels.ByteChannel +--java.nio.channels.WritableByteChannel +--java.nio.channels.ByteChannel +--java.nio.channels.GatheringByteChannel java.nio.channels.Slector java.nio.channels.ScatteringByteChannel, ByteChannel, GatheringByteChannel +--java.nio.channels.FileChannel +--java.nio.channels.SocketChannel +--java.nio.channels.DatagramChannel java.nio.charset.Charset java.nio.charset.CharsetEncoderjava.nio.charset.CharsetDecoder

NIO hierarchy diagram:

*** Basic concepts of data flow

Understanding data flow

Flow is generally divided into input stream and output stream, but this division is not absolute. A file, for example, is an output stream when data is written to it and an input stream when data is read from it. Of course, the keyboard is just a stream of numbers, and the screen is just an output stream.

◆ Standard data flow

Standard input and output refers to the way programs interact with the system in character mode (such as DOS), which is divided into three types:

Standard input studin, object is keyboard.

Standard output stdout, object is screen.

Standard error outputs stderr, the object is also the screen.

Example 8.1 Entering characters from the keyboard.

This example uses System.in.read(buffer) to input a line of characters from the keyboard, store it in the buffer, count stores the actual number of bytes read, and then outputs the value in the buffer in two ways: integer and character. The Read method is in the java.io package and throws an IOException. The procedure is as follows:

import java.io.*; public class Input1 { public static void main(String args[]) throws IOException { System.out.println("Input: "); byte buffer[] = new byte[512]; //Input buffer int count = System.in.read (buffer); //Read standard input stream System.out.println("Output: "); for (int i=0;i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report