Home Java javaTutorial Java language goes deep into the essence of JAVA IO flow

Java language goes deep into the essence of JAVA IO flow

Dec 17, 2016 am 11:29 AM

1.Input and Output
1.stream represents any data source capable of producing data, or any receiving source capable of receiving data.
In Java's IO, all streams (including Input and Out streams) include two types:
1.1 Byte-oriented stream
Byte-oriented stream means that bytes are extracted from the stream Read or write information to the stream. Byte-oriented streams include the following types:
1) input stream:
1) ByteArrayInputStream: use a buffer in memory as an InputStream
2) StringBufferInputStream: use a String object as an InputStream
3) FileInputStream: Use a file as an InputStream to realize the reading operation of the file
4) PipedInputStream: implements the concept of pipe, mainly used in threads
5) SequenceInputStream: merges multiple InputStreams into one InputStream
2) Out stream
1) ByteArrayOutputStream: Store information in a buffer in memory
2) FileOutputStream: Store information in a file
3) PipedOutputStream: Implement the concept of pipe, mainly used in threads
4) SequenceOutputStream: Merge multiple OutStreams It is an OutStream
1.2 Unicode character-oriented stream
Unicode character-oriented stream, which means reading or writing information from the stream in units of Unicode characters. Unicode character-oriented streams include the following types:
1) Input Stream
1) CharArrayReader: corresponds to ByteArrayInputStream
2) StringReader: corresponds to StringBufferInputStream
3) FileReader: corresponds to FileInputStream
4) PipedReader: corresponds to PipedInputStream
2) Out Stream
1) CharArrayWrite: Corresponds to ByteArrayOutputStream
2) StringWrite: There is no corresponding byte-oriented stream
3) FileWrite: Corresponds to FileOutputStream
4) PipedWrite: Corresponds to PipedOutputStream
Character-based A oriented stream basically has a corresponding byte-oriented stream. The functions implemented by the two corresponding classes are the same, but the guidance during operation is different. For example, CharArrayReader: and ByteArrayInputStream both use a buffer in memory as an InputStream. The difference is that the former reads one byte of information from memory each time, while the latter reads one character from memory each time.
1.3 Conversion between two non-currently oriented streams
InputStreamReader and OutputStreamReader: Convert a byte-oriented stream into a character-oriented stream.
2. Add attributes to stream
2.1 The role of "add attributes to stream"
Using the API for operating IO in Java introduced above, we can complete any operation we want to complete. But through subclasses of FilterInputStream and FilterOutStream, we can add properties to the stream. Below is an
example to illustrate the function of this function.
If we want to write data to a file, we can do this:
FileOutStream fs = new FileOutStream("test.txt");
Then we can call the write() function to and from the test.txt file through the generated fs object Data is written into it. However, if we want to implement the function of "first caching the data to be written to the file into the memory, and then writing the data in the cache to the file", none of the above APIs can meet our needs. But through subclasses of FilterInputStream and FilterOutStream, we can add the functions we need to FileOutStream.
2.2 Various types of FilterInputStream
2.2.1 Used to encapsulate byte-oriented InputStream
1) DataInputStream: Read basic type (int, char, etc.) data from the stream.
2) BufferedInputStream: Use buffer
3) LineNumberInputStream: It will record the number of lines in the input stream, and then you can call getLineNumber() and setLineNumber(int)
4) PushbackInputStream: Rarely used, generally used for compiler development
2.2.2 Used to encapsulate character-oriented InputStream
1) There is no class corresponding to DataInputStream. Unless you use BufferedReader instead when using readLine(), use DataInputStream
2) BufferedReader: corresponds to BufferedInputStream
3) LineNumberReader: corresponds to LineNumberInputStream
4) PushBackReader: corresponds to PushbackInputStream
2.3 Various types of FilterOutStream
2.2.3 Used to encapsulate byte-oriented OutputStream
1) DataIOutStream: Output basic type (int, char, etc.) data into the stream.
2) BufferedOutStream: Use buffer
3) PRintStream: Generate formatted output
2.2.4 Used to encapsulate character-oriented OutputStream
1) BufferedWrite: Corresponds to
2) PrintWrite: Corresponds to
3. RandomaccessFile
1 ) Read and write files can be completed through the RandomAccessFile object
2) When generating an object, you can specify the nature of the file to be opened: r, read-only; w, write-only; rw can be read and written
3) You can jump directly to the location specified in the file
4. An example of I/O application
import java.io.*;
public class TestIO{
public static void main(String[] args)
throws IOException{
//1. Read from a file in row units Data
BufferedReader in =
new BufferedReader(
new FileReader("F:\nepalon\TestIO.java"));
String s, s2 = new String();
while((s = in.readLine()) ! = null)
s2 += s + "n";
in.close();

//1b. Receive keyboard input
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
System .out.println("Enter a line:");
System.out.println(stdin.readLine());

//2. Read data from a String object
StringReader in2 = new StringReader(s2) ;
int c;
while((c = in2.read()) != -1)
System.out.println((char)c);
in2.close();

The above is Java, IO Streaming content, please pay attention to the PHP Chinese website (www.php.cn) for more related articles!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

How to Run Your First Spring Boot Application in Spring Tool Suite? How to Run Your First Spring Boot Application in Spring Tool Suite? Feb 07, 2025 pm 12:11 PM

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo

See all articles