Lớp Java InputStreamReader

Trong hướng dẫn này, chúng ta sẽ tìm hiểu về Java InputStreamReader và các phương thức của nó với sự trợ giúp của các ví dụ.

Các InputStreamReaderlớp của java.iogói có thể được sử dụng để chuyển đổi dữ liệu theo byte vào dữ liệu trong ký tự.

Nó mở rộng lớp trừu tượng Reader.

Các InputStreamReaderlớp học làm việc với các dòng đầu vào khác. Nó còn được gọi là cầu nối giữa các luồng byte và các luồng ký tự. Điều này là do các InputStreamReaderbyte đọc từ luồng đầu vào dưới dạng ký tự.

Ví dụ: một số ký tự yêu cầu 2 byte để được lưu trữ trong bộ nhớ. Để đọc dữ liệu như vậy, chúng ta có thể sử dụng trình đọc luồng đầu vào đọc 2 byte cùng nhau và chuyển đổi thành ký tự tương ứng.

Tạo một InputStreamReader

Để tạo một InputStreamReaderjava.io.InputStreamReadertrước tiên chúng ta phải nhập gói. Khi chúng tôi nhập gói ở đây là cách chúng tôi có thể tạo trình đọc luồng đầu vào.

// Creates an InputStream
FileInputStream file = new FileInputStream(String path);

// Creates an InputStreamReader
InputStreamReader input = new InputStreamReader(file);

Trong ví dụ trên, chúng tôi đã tạo một đầu vàoInputStreamReader được đặt tên cùng với tệp được đặt tên .FileInputStream

Ở đây, dữ liệu trong tệp được lưu trữ bằng cách sử dụng một số mã hóa ký tự mặc định.

Tuy nhiên, chúng tôi cũng có thể chỉ định loại mã hóa ký tự ( UTF8 hoặc UTF16 ) trong tệp.

// Creates an InputStreamReader specifying the character encoding
InputStreamReader input = new InputStreamReader(file, Charset cs);

Ở đây, chúng tôi đã sử dụng Charsetlớp để chỉ định mã hóa ký tự trong tệp.

Các phương thức của InputStreamReader

Các InputStreamReaderlớp học cung cấp triển khai cho các phương pháp khác nhau trình bày trong Readerlớp.

phương thức read ()

  • read() – reads a single character from the reader
  • read(char[] array) – reads the characters from the reader and stores in the specified array
  • read(char[] array, int start, int length) – reads the number of characters equal to length from the reader and stores in the specified array starting from the start

Ví dụ, giả sử chúng ta có một tệp tên input.txt với nội dung như sau.

This is a line of text inside the file.

Hãy thử đọc tệp này bằng cách sử dụng InputStreamReader.

import java.io.InputStreamReader;
import java.io.FileInputStream;

class Main {
  public static void main(String[] args) {

    // Creates an array of character
    char[] array = new char[100];

    try {
      // Creates a FileInputStream
      FileInputStream file = new FileInputStream("input.txt");

      // Creates an InputStreamReader
      InputStreamReader input = new InputStreamReader(file);

      // Reads characters from the file
      input.read(array);
      System.out.println("Data in the stream:");
      System.out.println(array);

      // Closes the reader
      input.close();
    }

    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Đầu ra

Data in the stream:
This is a line of text inside the file.

Trong ví dụ trên, chúng tôi đã tạo một trình đọc luồng đầu vào bằng cách sử dụng luồng đầu vào tệp. Trình đọc luồng đầu vào được liên kết với tệp input.txt .

 FileInputStream file = new FileInputStream("input.txt");
 InputStreamReader input = new InputStreamReader(file);

Để đọc các ký tự từ tệp, chúng tôi đã sử dụng read()phương pháp.

Phương thức getEncoding ()

Các getEncoding()phương pháp có thể được sử dụng để có được các loại mã hóa được sử dụng để lưu trữ dữ liệu trong dòng đầu vào. Ví dụ,

import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.io.FileInputStream;

class Main {
  public static void main(String[] args) {

    try {
      // Creates a FileInputStream
      FileInputStream file = new FileInputStream("input.txt");

      // Creates an InputStreamReader with default encoding
      InputStreamReader input1 = new InputStreamReader(file);

      // Creates an InputStreamReader specifying the encoding
      InputStreamReader input2 = new InputStreamReader(file, Charset.forName("UTF8"));

      // Returns the character encoding of the input stream
      System.out.println("Character encoding of input1: " + input1.getEncoding());
      System.out.println("Character encoding of input2: " + input2.getEncoding());

      // Closes the reader
      input1.close();
      input2.close();
    }

    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Đầu ra

The character encoding of input1: Cp1252
The character encoding of input2: UTF8

Trong ví dụ trên, chúng ta đã tạo 2 đầu đọc luồng đầu vào có tên là input1 và input2 .

  • input1 does not specify the character encoding. Hence the getEncoding() method returns the canonical name of the default character encoding.
  • input2 specifies the character encoding, UTF8. Hence the getEncoding() method returns the specified character encoding.

Lưu ý : Chúng tôi đã sử dụng Charset.forName()phương pháp để chỉ định loại mã hóa ký tự. Để tìm hiểu thêm, hãy truy cập Java Charset (tài liệu Java chính thức) .

phương thức close ()

Để đóng trình đọc luồng đầu vào, chúng ta có thể sử dụng close()phương pháp. Khi close()phương thức được gọi, chúng ta không thể sử dụng trình đọc để đọc dữ liệu.

Các phương thức InputStreamReader khác

MethodDecfscription
ready()checks if the stream is ready to be read
mark()mark the position in stream up to which data has been read
reset()returns the control to the point in the stream where the mark was set

Để tìm hiểu thêm, hãy truy cập Java InputStreamReader (tài liệu Java chính thức) .









Gõ tìm kiếm nhanh...