search
HomeJavajavaTutorialJava language goes deep into the essence of JAVA IO flow

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
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
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function