Lớp Java InputStream

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

Các InputStreamlớp của java.iogói là một lớp cha trừu tượng đại diện cho một dòng đầu vào của byte.

Vì InputStreamlà một lớp trừu tượng nên bản thân nó không hữu ích. Tuy nhiên, các lớp con của nó có thể được sử dụng để đọc dữ liệu.

Các lớp con của InputStream

Để sử dụng chức năng của InputStream, chúng ta có thể sử dụng các lớp con của nó. Một số trong số đó là:

  • FileInputStream
  • ByteArrayInputStream
  • ObjectInputStream

Chúng ta sẽ tìm hiểu về tất cả các lớp con này trong hướng dẫn tiếp theo.

Tạo luồng đầu vào

Để tạo InputStream, java.io.InputStreamtrước tiên chúng ta phải nhập gói. Sau khi chúng tôi nhập gói, đây là cách chúng tôi có thể tạo luồng đầu vào.

// Creates an InputStream
InputStream object1 = new FileInputStream();

Ở đây, chúng tôi đã tạo một luồng đầu vào bằng cách sử dụng FileInputStream. Đó là vì InputStreamlà một lớp trừu tượng. Do đó chúng ta không thể tạo một đối tượng của InputStream.

Lưu ý : Chúng ta cũng có thể tạo một luồng đầu vào từ các lớp con khác của InputStream.

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

Các InputStreamlớp học cung cấp phương pháp khác nhau được thực hiện bởi lớp con của nó. Dưới đây là một số phương pháp thường được sử dụng:

  • read() – reads one byte of data from the input stream
  • read(byte[] array) – reads bytes from the stream and stores in the specified array
  • available() – returns the number of bytes available in the input stream
  • mark() – marks the position in the input stream up to which data has been read
  • reset() – returns the control to the point in the stream where the mark was set
  • markSupported() – checks if the mark() and reset() method is supported in the stream
  • skips() – skips and discards the specified number of bytes from the input stream
  • close() – closes the input stream

Ví dụ: InputStream Sử dụng FileInputStream

Đây là cách chúng ta có thể triển khai InputStreambằng cách sử dụng FileInputStreamlớp.

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 FileInputStream(một lớp con của InputStream).

import java.io.FileInputStream;
import java.io.InputStream;

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

    byte[] array = new byte[100];

    try {
      InputStream input = new FileInputStream("input.txt");

      System.out.println("Available bytes in the file: " + input.available());

      // Read byte from the input stream
      input.read(array);
      System.out.println("Data read from the file: ");

      // Convert byte array into string
      String data = new String(array);
      System.out.println(data);

      // Close the input stream
      input.close();
    } catch (Exception e) {
      e.getStackTrace();
    }
  }
}

Đầu ra

Available bytes in the file: 39
Data read from the file:
This is a line of text inside the file

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

InputStream input = new FileInputStream("input.txt");

Để đọc dữ liệu từ tệp input.txt , chúng tôi đã thực hiện hai phương pháp này.

input.read(array);  // to read data from the input stream
input.close();            // to close the input stream

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









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