Home > Java > javaTutorial > Using the Java language character-based streams

Using the Java language character-based streams

Susan Sarandon
Release: 2024-11-09 08:23:02
Original
725 people have browsed it

To perform character-based I/O operations, Java provides its own hierarchy of character-based streams, with abstract classes such as Reader and Writer. These classes allow you to read and write characters directly, making them more suitable for text data than byte streams. The main methods of these classes handle read and write operations and can throw IOException in case of an error.

Character Flow Structure

Main Abstract Classes:

  • Reader: Base for reading characters.
  • Writer: Base for writing characters.

These classes form the minimal structure of I/O operations for character streams, with methods applicable to all subclasses.

Console Input with Character Streams
For internationalized programs or programs that manipulate text, it is preferable to read characters from the console using character streams. Since System.in is a byte stream, it needs to be adapted for character streams.

For this, we use:

  • InputStreamReader: Converts bytes to characters.
  • BufferedReader: Improves efficiency by buffering input.

Example of Reading Console Input
To read console input with BufferedReader, we first convert System.in to a character stream using InputStreamReader:

import java.io.*;

public class ConsoleReaderExample {
  public static void main(String args[]) {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
      System.out.print("Digite uma linha de texto: ");
      String linha = reader.readLine();
      System.out.println("Você digitou: " + linha);
    } catch(IOException e) {
      System.out.println("Erro de entrada/saída: " + e);
    }
  }
}

Copy after login

Code Explanation

  • InputStreamReader: Converts the byte stream from System.in to a character stream.
  • BufferedReader: Used for efficient reading of characters, strings and lines.

Advantages of Character Flows

  • Internationalization: Compatible with different character sets.
  • Efficiency: BufferedReader allows you to read an entire line at once, simplifying the process and reducing system calls.

These character streams make text processing easier and are ideal for data entry and file manipulation where characters and text are the main focus.

Usando os fluxos baseados em caracteres da linguagem Java

The above is the detailed content of Using the Java language character-based streams. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template