Home Java Javagetting Started What is the difference between java streams

What is the difference between java streams

Nov 14, 2019 am 09:36 AM
java the difference flow

What is the difference between java streams

All data in the program is transmitted or saved in the form of streams. When the program needs data, it must use the input stream to read the data, and when the program needs some data When saving, it is done using the output stream.

The input and output in the program are all saved in the form of streams, and what is saved in the streams are actually byte files.

Byte stream and character stream

There are two main categories of operating file content in the java.io package: byte stream and character stream, both of which are Divided into input and output operations. Outputting data in a byte stream is mainly done using OutputStream, and input is done using InputStream. Outputting in a character stream is mainly done using the Writer class, and input streams are mainly done using the Reader class. (These four are abstract classes)

Java provides a package dedicated to input and output functions, Java.io, including:

InputStream, OutputStream, Reader, Writer

InputStream and OutputStream are designed for byte streams and are mainly used to process bytes or binary objects.

Reader and Writer are designed for character streams (one character occupies two bytes) and are mainly used to process characters or strings.

The character stream processing unit is a 2-byte Unicode character, operating on characters, character arrays or strings respectively, while the byte stream processing unit is 1 byte, operating on bytes and byte arrays. Therefore, the character stream is formed by the Java virtual machine converting bytes into 2-byte Unicode characters, so it has better support for multiple languages! If it is audio files, pictures, and songs, it is better to use byte streams. If it is related to Chinese (text), it is better to use character streams.

All files are stored in bytes. What is retained on the disk is not the characters of the file, but the characters are first encoded into bytes, and then these bytes are stored on the disk. When reading a file (especially a text file), it is also read byte by byte to form a byte sequence.

Byte stream can be used for any type of object, including binary objects, while character stream can only process characters or strings; 2. Byte stream provides the function of processing any type of IO operation, but it cannot Handles Unicode characters directly, and character streams do just that.

Byte stream is the most basic, all subclasses of InputStrem and OutputStream are, mainly used to process binary data, it is processed by bytes, but in reality a lot of data is text, and The concept of character stream is proposed, which is processed according to the encoding of the virtual machine, that is, the character set is converted. The two are related through InputStreamReader and OutputStreamWriter. In fact, they are related through byte[] and String. Chinese character problems that arise during development are actually caused by the inconsistency in conversion between character streams and byte streams.

The return type of the read() method of the Reader class is int: the character read as an integer (accounting for 16 bits in two bytes), ranging from 0 to 65535 (0x00-0xffff), if When the end of the stream is reached, -1 is returned.

Although inputStream's read() also returns int, since this class is oriented to byte streams and one byte occupies 8 bits, it returns an int byte value in the range of 0 to 255. If no bytes are available because the end of the stream has been reached, the value -1 is returned. Therefore, values ​​that cannot be represented by 0-255 must be read using a character stream! For example, Chinese characters.

php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!

The above is the detailed content of What is the difference between java streams. For more information, please follow other related articles on the PHP Chinese website!

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)

What are the basic requirements for c language functions What are the basic requirements for c language functions Apr 03, 2025 pm 10:06 PM

C language functions are the basis for code modularization and program building. They consist of declarations (function headers) and definitions (function bodies). C language uses values ​​to pass parameters by default, but external variables can also be modified using address pass. Functions can have or have no return value, and the return value type must be consistent with the declaration. Function naming should be clear and easy to understand, using camel or underscore nomenclature. Follow the single responsibility principle and keep the function simplicity to improve maintainability and readability.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

How to set password protection for export PDF on PS How to set password protection for export PDF on PS Apr 06, 2025 pm 04:45 PM

Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

Concept of c language function Concept of c language function Apr 03, 2025 pm 10:09 PM

C language functions are reusable code blocks. They receive input, perform operations, and return results, which modularly improves reusability and reduces complexity. The internal mechanism of the function includes parameter passing, function execution, and return values. The entire process involves optimization such as function inline. A good function is written following the principle of single responsibility, small number of parameters, naming specifications, and error handling. Pointers combined with functions can achieve more powerful functions, such as modifying external variable values. Function pointers pass functions as parameters or store addresses, and are used to implement dynamic calls to functions. Understanding function features and techniques is the key to writing efficient, maintainable, and easy to understand C programs.

What are the differences and connections between c and c#? What are the differences and connections between c and c#? Apr 03, 2025 pm 10:36 PM

Although C and C# have similarities, they are completely different: C is a process-oriented, manual memory management, and platform-dependent language used for system programming; C# is an object-oriented, garbage collection, and platform-independent language used for desktop, web application and game development.

How to use XPath to search from a specified DOM node in JavaScript? How to use XPath to search from a specified DOM node in JavaScript? Apr 04, 2025 pm 11:15 PM

Detailed explanation of XPath search method under DOM nodes In JavaScript, we often need to find specific nodes from the DOM tree based on XPath expressions. If you need to...

Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Apr 05, 2025 pm 01:03 PM

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

The difference in output results of console.log: Why do the same variables have different printing methods but different results? The difference in output results of console.log: Why do the same variables have different printing methods but different results? Apr 04, 2025 am 11:48 AM

In-depth discussion of the differences in console.log output in this article will analyze the reasons why the output results of console.log function in a piece of code are different. Code snippets involve URL parameter resolution...

See all articles