Lớp Java StringReader

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

Các StringReaderlớp của java.iogói có thể được sử dụng để đọc dữ liệu (bằng ký tự) từ chuỗi.

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

Lưu ý : Trong StringReader, chuỗi được chỉ định hoạt động như một nguồn từ đó các ký tự được đọc riêng lẻ.

Tạo một StringReader

Để tạo một StringReaderjava.io.StringReadertrướ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 chuỗi.

// Creates a StringReader
StringReader input = new StringReader(String data);

Ở đây, chúng ta đã tạo một hàm StringReaderđọc các ký tự từ dữ liệu có tên chuỗi được chỉ định .

Phương thức của StringReader

Các StringReaderlớ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 string 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 position start

Ví dụ: Java StringReader

import java.io.StringReader;

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

    String data = "This is the text read from StringReader.";

    // Create a character array
    char[] array = new char[100];

    try {
      // Create a StringReader
      StringReader input = new StringReader(data);

      //Use the read method
      input.read(array);
      System.out.println("Data read from the string:");
      System.out.println(array);

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

Đầu ra

Data read from the string:
This is the text read from StringReader.

Trong ví dụ trên, chúng ta đã tạo một trình đọc chuỗi có tên là đầu vào . Trình đọc chuỗi được liên kết với dữ liệu chuỗi .

String data = "This is a text in the string.";
StringReader input = new StringReader(data);

Để đọc dữ liệu từ chuỗi, chúng tôi đã sử dụng read()phương thức.

Ở đây, phương thức đọc một mảng ký tự từ trình đọc và lưu trữ trong mảng được chỉ định.

phương thức bỏ qua ()

Để loại bỏ và bỏ qua số ký tự đã chỉ định, chúng ta có thể sử dụng skip()phương pháp này. Ví dụ,

import java.io.StringReader;

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

    String data = "This is the text read from StringReader";
    System.out.println("Original data: " + data);

    // Create a character array
    char[] array = new char[100];

    try {
      // Create a StringReader
      StringReader input = new StringReader(data);

      // Use the skip() method
      input.skip(5);

      //Use the read method
      input.read(array);
      System.out.println("Data after skipping 5 characters:");
      System.out.println(array);

      input.close();
    }

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

Đầu ra

Original data: This is the text read from the StringReader
Data after skipping 5 characters:
is the text read from the StringReader

Trong ví dụ trên, chúng tôi đã sử dụng skip()phương pháp để bỏ qua 5 ký tự khỏi trình đọc chuỗi. Do đó, các nhân vật 'T''h''i''s'và ' 'được bỏ qua từ người đọc chuỗi gốc.

phương thức close ()

Để đóng trình đọc chuỗi, chúng ta có thể sử dụng close()phương thức. 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 từ chuỗi.

Các phương pháp khác của StringReader

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

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









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