Sunday, 11 June 2017

Java Tutorial : Java IO (StringWriter constructor accept initialSize) ~ foundjava


Click here to watch in Youtube :



StringWriterDemo.java

import java.io.StringWriter;

/*
 * public StringWriter(int initialSize)
 * 
 * Parameters: 
 * ----------
 * 
 * initialSize - The number of char values that will fit
 * into this buffer before it is automatically expanded
 */

public class StringWriterDemo
{

    public static void main(String[] args) throws Exception
    {
        StringWriter stringWriter = null;
        try
        {
            /*
             * Create a new string writer using the
             * specified initial string-buffer size.
             */
            stringWriter = new StringWriter(32);

            // write characters to writer.
            stringWriter.write("Welcome to India");

            /*
             * Return the buffer's current value as a
             * string.
             */
            String str = stringWriter.toString();
            System.out.println("str = " + str);
            /*
             * Returns: StringBuffer holding the current
             * buffer value.
             */
            StringBuffer sb = stringWriter.getBuffer();
            System.out.println("sb = " + sb);

        }
        finally
        {
            if (stringWriter != null)
            {
                stringWriter.close();
            }
        }
    }
}
Output
str = Welcome to India
sb = Welcome to India

Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment