English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dans ce tutoriel, nous allons apprendre Java StringWriter et ses sous-classes à l'aide d'exemples.
La classe StringWriter du paquet java.io peut être utilisée pour écrire des données (en unités de caractères) dans le tampon de chaîne.
Il hérite de la classe abstraite Writer.
注意:En Java, le tampon de chaîne est considéré comme une chaîne mutable. C'est-à-dire, nous pouvons modifier le tampon de chaîne. Pour convertir un tampon de chaîne en chaîne, vous pouvez utiliser la méthode toString().
Pour créer un StringWriter, nous devons d'abord importer le paquet java.io.StringWriter. Après avoir importé le paquet, nous pouvons créer un écrivain de chaîne.
//Créer un StringWriter StringWriter output = new StringWriter();
Ici, nous avons créé un écrivain de chaîne avec une capacité de tampon par défaut. Mais, nous pouvons également spécifier la capacité du tampon de chaîne.
//Créer un StringWriter avec une capacité de tampon de chaîne spécifiée StringWriter output = new StringWriter(int size);
Ici, size spécifie la capacité du tampon de chaîne.
StringWriter类为Writer类中提供的不同方法提供实现。
write() - 向字符串写入器写入一个字符
write(char[] array) - 将指定数组中的字符写入写入器
write(String data) - 将指定的字符串写入写入器
import java.io.StringWriter; public class Main { public static void main(String[] args) { String data = "This is the text in the string."; try { //创建具有默认字符串缓冲区容量的StringWriter StringWriter output = new StringWriter(); //将数据写入字符串缓冲区 output.write(data); //打印字符串写入器 System.out.println("StringWriter中的数据: " ) + output); output.close(); } catch(Exception e) { e.getStackTrace(); } } }
Output result
StringWriter中的数据: This is the text in the string.
在上面的示例中,我们创建了一个名为的字符串写入器output。
StringWriter output = new StringWriter();
然后,我们使用该write()方法将字符串数据写入字符串缓冲区。
注意:我们已使用该toString()方法从字符串缓冲区以字符串形式获取输出数据。
getBuffer() -返回字符串缓冲区中存在的数据
toString() -将字符串缓冲区中存在的数据作为字符串返回
例如,
import java.io.StringWriter; public class Main { public static void main(String[] args) { String data = "This is the original data"; try { //创建具有默认字符串缓冲区容量的StringWriter StringWriter output = new StringWriter(); //将数据写入字符串缓冲区 output.write(data); //返回字符串缓冲区 StringBuffer stringBuffer = output.getBuffer(); System.out.println("StringBuffer: " ) + stringBuffer); //以字符串形式返回字符串缓冲区 String string = output.toString(); System.out.println("String: " ) + string); output.close(); } catch(Exception e) { e.getStackTrace(); } } }
Output result
StringBuffer: This is the original data String: This is the original data
Here, we use the getBuffer() method to obtain the data existing in the string buffer. Moreover, the toString() method also returns the data existing in the string buffer in string form.
To close the string writer, we can use the close() method.
However, the close() method is invalid in the StringWriter class. Even if the close() method is called, we can still use the methods of this class.
Method | Description |
---|---|
flush() | Force all data existing in the writer to be written into the string buffer |
append() | Insert the specified character into the current writer |