English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dans ce tutoriel, nous allons apprendre Java StringReader et ses méthodes à l'aide d'exemples.
La classe StringReader du paquet java.io peut être utilisée pour lire des données à partir de la chaîne (par unité de caractère).
Il hérite de la classe abstraite Reader.
AttentionDans StringReader, la chaîne spécifiée joue le rôle de source, à partir de laquelle les caractères sont lus séparément.
Pour créer un StringReader, nous devons d'abord importer le paquet java.io.StringReader. Après avoir importé le paquet, nous pouvons créer le lecteur de chaîne.
//Création de StringReader StringReader input = new StringReader(String data);
Ici, nous créons un StringReader qui lit des caractères à partir de la chaîne de caractères nommée data.
La classe StringReader fournit des implémentations pour différentes méthodes de la classe Reader.
read() - Lecture d'un caractère à partir du lecteur de chaîne
read(char[] array) - Lecture de caractères à partir du lecteur et stockage dans le tableau spécifié
read(char[] array, int start, int length) - Lecture d'un nombre de caractères égal à length, et stockage dans le tableau spécifié à partir de la position start
import java.io.StringReader; public class Main { public static void main(String[] args) { String data = "This is the text read from StringReader."; //Création d'un tableau de caractères char[] array = new char[100]; try { //Création d'un StringReader StringReader input = new StringReader(data); //Utilisation de la méthode read input.read(array); System.out.println("Lecture de données à partir de la chaîne:"); System.out.println(array); input.close(); } catch(Exception e) { e.getStackTrace(); } } }
Output result
Lecture de données à partir de la chaîne This is the text read from StringReader.
Dans l'exemple ci-dessus, nous avons créé un lecteur de chaîne nommé input. Le lecteur de chaîne est lié aux données de chaîne (data).
String data = "This is a text in the string."; StringReader input = new StringReader(data);
Pour lire des données à partir de la chaîne, nous utilisons la méthode read().
Ici, cette méthode lit des caractères à partir du lecteur et les stocke dans le tableau spécifié.
Pour ignorer et sauter un certain nombre de caractères, vous pouvez utiliser la méthode skip(). Par exemple
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("Données originales : "); + data); //Création d'un tableau de caractères char[] array = new char[100]; try { //Création de StringReader StringReader input = new StringReader(data); //Utilisation de la méthode skip() input.skip(5); //Utilisation de la méthode read input.read(array); System.out.println("Sauter5les données après le caractère : "); System.out.println(array); input.close(); } catch(Exception e) { e.getStackTrace(); } } }
Output result
Original data: This is the text read from the StringReader Skip5After the character data: is the text read from the StringReader
In the above example, we use the skip() method to skip5characters. Therefore, the characters 'T', 'h', 'i', 's', and ' ' are skipped from the original string reader.
To close the string reader, we can use the close() method. After calling the close() method, we will not be able to use the reader to read data from the string.
Method | Description |
---|---|
ready() | Check if the string reader is ready to be read |
mark() | The position of the data read from the marker reader |
reset() | Reset the marker, return to the position set by the reader |