Trong hướng dẫn này, chúng ta sẽ tìm hiểu về Java Reader, các lớp con và các phương thức của nó với sự trợ giúp của một ví dụ.
Các Reader
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 ký tự.
Vì Reader
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 Reader
Để sử dụng chức năng của Reader
, chúng ta có thể sử dụng các lớp con của nó. Một số trong số đó là:
- BufferedReader
- InputStreamReader
- FileReader
- StringReader
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 trình đọc
Để tạo một Reader
, java.io.Reader
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 trình đọc.
// Creates a Reader
Reader input = new FileReader();
Ở đây, chúng tôi đã tạo một trình đọc bằng cách sử dụng FileReader
lớp. Đó là vì Reader
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 Reader
.
Lưu ý : Chúng tôi cũng có thể tạo trình đọc từ các lớp con khác của Reader
.
Phương pháp của người đọc
Các Reader
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:
ready()
– checks if the reader is ready to be readread(char[] array)
– reads the characters from the stream and stores in the specified arrayread(char[] array, int start, int length)
– reads the number of characters equal to length from the stream and stores in the specified array starting from the startmark()
– marks the position in the stream up to which data has been readreset()
– returns the control to the point in the stream where the mark is setskip()
– discards the specified number of characters from the stream
Ví dụ: Trình đọc bằng FileReader
Đây là cách chúng ta có thể triển khai Reader
bằng cách sử dụng FileReader
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 FileReader
(một lớp con của Reader
).
import java.io.Reader;
import java.io.FileReader;
class Main {
public static void main(String[] args) {
// Creates an array of character
char[] array = new char[100];
try {
// Creates a reader using the FileReader
Reader input = new FileReader("input.txt");
// Checks if reader is ready
System.out.println("Is there data in the stream? " + input.ready());
// Reads characters
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
Is there data in the stream? true 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 bằng cách sử dụng FileReader
lớp. Trình đọc được liên kết với tệp input.txt .
Reader input = new FileReader("input.txt");
Để đọc dữ liệu từ tệp input.txt , chúng tôi đã triển khai các phương pháp này.
input.read(); // to read data from the reader
input.close(); // to close the reader
Để tìm hiểu thêm, hãy truy cập Java Reader (tài liệu Java chính thức) .