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 InputStream
lớp của java.io
gó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ì InputStream
là 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.InputStream
trướ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ì InputStream
là 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 InputStream
lớ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 streamread(byte[] array)
– reads bytes from the stream and stores in the specified arrayavailable()
– returns the number of bytes available in the input streammark()
– marks the position in the input stream up to which data has been readreset()
– returns the control to the point in the stream where the mark was setmarkSupported()
– checks if themark()
andreset()
method is supported in the streamskips()
– skips and discards the specified number of bytes from the input streamclose()
– closes the input stream
Ví dụ: InputStream Sử dụng FileInputStream
Đây là cách chúng ta có thể triển khai InputStream
bằng cách sử dụng FileInputStream
lớ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 FileInputStream
lớ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) .