write ajava program using buffer sign PLEASE HELP ME WITH THIS
Answers
Explanation:
StringBuffer is a peer class of String that provides much of the functionality of strings. String represents fixed-length, immutable character sequences while StringBuffer represents growable and writable character sequences.
StringBuffer may have characters and substrings inserted in the middle or appended to the end. It will automatically grow to make room for such additions and often has more characters preallocated than are actually needed, to allow room for growth.
StringBuffer Constructors
StringBuffer( ): It reserves room for 16 characters without reallocation.
StringBuffer s=new StringBuffer();
StringBuffer( int size)It accepts an integer argument that explicitly sets the size of the buffer.
StringBuffer s=new StringBuffer(20);
StringBuffer(String str): It accepts a String argument that sets the initial contents of the StringBuffer object and reserves room for 16 more characters without reallocation.
StringBuffer s=new StringBuffer("GeeksforGeeks");
Methods
Some of the most used methods are:
length( ) and capacity( ): The length of a StringBuffer can be found by the length( ) method, while the total allocated capacity can be found by the capacity( ) method.
Code Example:
import java.io.*;
class GFG {
public static void main(String[] args)
{
StringBuffer s = new StringBuffer("GeeksforGeeks");
int p = s.length();
int q = s.capacity();
System.out.println("Length of string GeeksforGeeks=" + p);
System.out.println("Capacity of string GeeksforGeeks=" + q);
}
}
Output:
Length of string GeeksforGeeks=13
Capacity of string GeeksforGeeks=29
append( ): It is used to add text at the end of the existence text. Here are a few of its forms:
StringBuffer append(String str)
StringBuffer append(int num)
Code Example:
import java.io.*;
class GFG {
public static void main(String[] args)
{
StringBuffer s = new StringBuffer("Geeksfor");
s.append("Geeks");
System.out.println(s); // returns GeeksforGeeks
s.append(1);
System.out.println(s); // returns GeeksforGeeks1
}
Answer:
Hi friend
Hope this answer helps you
Explanation:
Example of Java BufferedWriter
package com.javatpoint;
import java.io.*;
public class BufferedWriterExample {
public static void main(String[] args) throws Exception {
FileWriter writer = new FileWriter("D:\\testout.txt");
BufferedWriter buffer = new BufferedWriter(writer);
buffer.write("Welcome to javaTpoint.")