Java CharArrayWriter toString()方法及示例
Java中 CharArrayWriter 类的 toString() 方法将给定的输入数据转换为一个字符串。
语法:
public String[] toString()
参数: 该方法不接受任何参数。
返回值: 该方法返回一个输入数据的副本。
下面的程序说明了上述方法。
程序1 :
// Java program to illustrate the
// above mentioned method
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
// Initializing String Witer
CharArrayWriter geek_writer1
= new CharArrayWriter();
CharSequence char_sq1 = "geeks for + ";
CharSequence char_sq2 = "geeks";
// Use of append(CharSequence char_sq)
geek_writer1.append(char_sq1);
geek_writer1.append(char_sq2);
// Convert it into string and print
System.out.println("String : "
+ geek_writer1.toString());
}
}
输出
String : geeks for + geeks
程序2
// Java program to illustrate the
// above mentioned method
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
// Initializing String Witer
CharArrayWriter geek_writer1
= new CharArrayWriter();
CharSequence char_sq1 = "Gopal ";
CharSequence char_sq2 = "Dave";
// Use of append(CharSequence char_sq)
geek_writer1.append(char_sq1);
geek_writer1.append(char_sq2);
// Convert it into string and print
System.out.println("String : "
+ geek_writer1.toString());
}
}
输出
String : Gopal Dave
参考资料 : https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#toString()