Java 中 StringWriter getBuffer()方法,带示例

原文:https://www . geeksforgeeks . org/stringwriter-get buffer-in-Java-method-with-examples/

Java 中 StringWriter 类的 getBuffer() 方法用于获取这个 StringWriter 实例的 StringBuffer 表示。该方法不接受任何参数,返回所需的 StringBuffer 值。

语法:

public StringBuffer getBuffer()

参数:此方法接受不接受任何参数。

返回值:这个方法返回一个 StringBuffer,它是 StringWriter 实例的 StringBuffer 表示。

下面的方法说明了 getBuffer()方法的工作原理:

程序 1:

// Java program to demonstrate
// StringWriter getBuffer() method

import java.io.*;

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

        try {

            // Create a StringWriter instance
            StringWriter writer
                = new StringWriter();

            // Get the String
            // to be written in the stream
            String string = "GeeksForGeeks";

            // Write the the string
            // to this writer using write() method
            writer.write(string);

            // Get the StringBuffer of this StringWriter
            StringBuffer stringBuffer = writer.getBuffer();

            System.out.println("StringBuffer representation: "
                               + stringBuffer);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output:

StringBuffer representation: GeeksForGeeks

程序 2:

// Java program to demonstrate
// StringWriter getBuffer() method

import java.io.*;

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

        try {

            // Create a StringWriter instance
            StringWriter writer
                = new StringWriter();

            // Get the String
            // to be written in the stream
            String string = "GFG";

            // Write the the string
            // to this writer using write() method
            writer.write(string);

            // Get the StringBuffer of this StringWriter
            StringBuffer stringBuffer = writer.getBuffer();

            System.out.println("StringBuffer representation: "
                               + stringBuffer);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output:

StringBuffer representation: GFG