Home / JAVA / Convert Writer to String in Java

Convert Writer to String in Java

Here is the Java Program for Writer to String Conversion : If you want to convert writer to string java here is the example for StringWriter Stream that is to be imported from the package Java.io. Here we are using append function.

 

  • package com.getprogramcode.string;
    import java.io.StringWriter;
    
    public class WriterToString {
        public static void main(String args[]){
            // create an instance of writer class and store it to a variable
              StringWriter var = new StringWriter();
    
              // appending string-characters        
              var.append("Welcome to ");
              var.append("Getprogramcode.com");
              
              String temp = var.toString();
              System.out.println(temp);     // thus a writer is converted to string using java program
        }
    }
  • You can also create an instance of StringWriter Class and allocate it to an object of parent class Writer

    Writer writer = new StringWriter(); //
    String data = writer.toString(); 
    

    Rest will remain the same. Just the object creation and initialization can be different You can also make it using other functions like:

OUTPUT

Welcome to Getprogramcode.com

Check Also

Java program to implement polymorphism

Java program to implement polymorphism: Polymorphism is a technique in java with which you can ...

Leave a Reply

Your email address will not be published. Required fields are marked *