{"id":90033,"date":"2025-07-08T18:00:14","date_gmt":"2025-07-08T12:30:14","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=90033"},"modified":"2025-07-08T11:38:09","modified_gmt":"2025-07-08T06:08:09","slug":"java-stringwriter-class","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/","title":{"rendered":"Java StringWriter Class"},"content":{"rendered":"<p>The StringWriter class is a stream of characters that contains the output of a buffer string, which can be used to create a string. There&#8217;s no impact when you close the StringWriter. After the stream is shut down, methods can be called from this class without throwing an IOException. Constructors in the Java StringWriter class:<\/p>\n<p><strong>Below is the constructor used for Java&#8217;s StringWriter class:<\/strong><\/p>\n<p><strong>StringWriter():<\/strong> Creates a new string writer using the initial or default buffer size.<\/p>\n<p><strong>StringWriter(int size):<\/strong> Creates a new StringWriter using the specified.<\/p>\n<h2>Create a StringWriter<\/h2>\n<p>We must first ensure that we import the java.io.StringWriter package before creating a StringWriter. This is where we can get a string writer once the package has been imported.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"> \/\/ Creates a StringWriter\r\n The output of the StringWriter is new StringWriter; \r\nHere we're creating a string writer that has default buffer capacity. However, we can also define a buffer size for the string. \r\n\/\/ Initializes a string writer with specified buffer capacity \r\nStringWriter output= a new StringWriter(ints' size; \r\nThe size specifies the string buffer's capacity here.<\/pre>\n<h3>write() Method:<\/h3>\n<p>Write() writes a single character to the string writer. write(char[] array) &#8211; writes the characters from the specified array to the writer write(String data) &#8211; writes the specified string to the writer<\/p>\n<h4>Program:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.StringWriter;\r\npublic class TechVidvan {\r\n  public static void main(String[] args) {\r\n    String data = \"This is the text in the string.\";\r\nTry {\r\n      StringWriter output = new StringWriter();\r\n           output.write(data);\r\n      System.out.println(\"Data in StringWriter: \" + output)\r\n      output.close();\r\n    }\r\n\r\n    catch(Exception e) {\r\n      e.getStackTrace();\r\n    }\r\n  }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nData in the StringWriter: This is the text in the string.<\/p>\n<h3>Use a StringBuffer to access data:<\/h3>\n<p>The data that is present in the string buffer shall be returned by getBuffer(). When toString() is called, it returns the data in the buffer as a string.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.StringWriter;\r\npublic class TechVidvan {\r\n  public static void main(String[] args) {\r\n String data = \"This is the original data\";\r\n    Try {\r\n       StringWriter output = new StringWriter();\r\n           output.write(data);\r\n         StringBuffer stringBuffer = output.getBuffer();\r\n      System.out.println(\"StringBuffer: \" + stringBuffer);\r\n           String string = output.toString();\r\n      System.out.println(\"String: \" + string);\r\n      output.close();\r\n    }\r\n     catch(Exception e) {\r\n      e.getStackTrace();\r\n    }\r\n  }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nStringBuffer: This is the original data<br \/>\nString: This is the original data<\/p>\n<h3>Close() Method:<\/h3>\n<p>Close() will be used for closing the string writer. However, the StringWriter class is unaffected by a Close() method. Even after calling the close() function, we can call methods of this class.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">packet TechVidvan;\r\nimport java.io.BufferedReader;\r\nimport java.io.FileInputStream;\r\nimport java.io.InputStreamReader;\r\nimport java.io.StringWriter;\r\npublic class StudyTonight\r\n{\r\npublic static void main(String args[])\r\n{\r\nTry\r\n{\r\nchar[] arr = new char[512];\r\nStringWriter writer = new StringWriter();\r\nFileInputStream input = new FileInputStream(\"E:\\\\tech vidvan\\\\output.txt\");\r\nBufferedReader buffer = new BufferedReader(new InputStreamReader(input, \"UTF-8\"));\r\nint c;\r\nwhile ((c = buffer.read(arr)) != -1) {\r\nwriter.write(arr, 0, c);\r\n}\r\nSystem.out.println(writer.toString());\r\nwriter.close();\r\nbuffer.close();\r\n}\r\ncatch (exception e)\r\n{\r\nSystem.out.println(\"Error: \"+e.toString());\r\n}\r\n}\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nHello TechVidvan<\/p>\n<h3>A simple use case<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"> import java.io.BufferedReader;\r\nimport java.io.FileInputStream;\r\nimport java.io.IOException;\r\nimport java.io.InputStreamReader;\r\nimport java.io.Reader;\r\nimport java.io.StringWriter;\r\nimport java.io.Writer;\r\n \r\npublic class StringWriterExample {\r\n    public static void main(String[] args) throws IOException {\r\n        String str1 = stringWriter(\"F:\\\\nikos7\\\\Desktop\\\\s.txt\");\r\n        System.out.println(str1);\r\n    }\r\n    public static String stringWriter(String fileName) throws IOException {\r\n        char[] buff = new Char[1024];\r\n        Writer stringWriter = new StringWriter();\r\n        FileInputStream fStream = null;\r\n        Reader bReader = null;\r\n        Try {\r\n           fStream = new FileInputStream(fileName);\r\n            bReader = new BufferedReader(new InputStreamReader(fStream, \"UTF-8\"));\r\n            int n;\r\n            while ((n = bReader.read(buff)) != -1) {\r\n                stringWriter.write(buff, 0, n);\r\n            }\r\n        } Finally {\r\n            bReader.close();\r\n            stringWriter.close();\r\n            fStream.close();\r\n        }\r\n        return stringWriter.toString();\r\n    }\r\n}<\/pre>\n<h3>Advantage<\/h3>\n<ul>\n<li>The use of Sfring methods to work with their content is easy.<\/li>\n<li>The use of an &#8220;+&#8221; operator allows for a higher performance relative to classical string concatenation.<\/li>\n<li>New string objects have been created for each concatenation, which leads to unallocated memory and object creation when consolidating multiple strings using the Windows+&#8221; operator.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>Java&#8217;s StringWriter class. The Java StringWriter class is a character stream that takes output from the string buffer, for example, when you want to construct a string.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The StringWriter class is a stream of characters that contains the output of a buffer string, which can be used to create a string. There&#8217;s no impact when you close the StringWriter. After the&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447382,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,440,441,263,327,250,442,443],"class_list":["post-90033","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-stringwriter","tag-java-stringwriter-class","tag-java-tutorial-for-beginners","tag-java-tutorials","tag-learn-java","tag-stringwriter-class-in-java","tag-stringwriter-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java StringWriter Class - TechVidvan<\/title>\n<meta name=\"description\" content=\"The Java StringWriter class is a stream of characters that contains the output of a buffer string, which can be used to create a string.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java StringWriter Class - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"The Java StringWriter class is a stream of characters that contains the output of a buffer string, which can be used to create a string.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-08T12:30:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-stringwriter.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java StringWriter Class - TechVidvan","description":"The Java StringWriter class is a stream of characters that contains the output of a buffer string, which can be used to create a string.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/","og_locale":"en_US","og_type":"article","og_title":"Java StringWriter Class - TechVidvan","og_description":"The Java StringWriter class is a stream of characters that contains the output of a buffer string, which can be used to create a string.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2025-07-08T12:30:14+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-stringwriter.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java StringWriter Class","datePublished":"2025-07-08T12:30:14+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/"},"wordCount":347,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-stringwriter.webp","keywords":["java","java stringwriter","java stringwriter class","java tutorial for beginners","java tutorials","Learn Java","stringwriter class in java","stringwriter in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/","url":"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/","name":"Java StringWriter Class - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-stringwriter.webp","datePublished":"2025-07-08T12:30:14+00:00","description":"The Java StringWriter class is a stream of characters that contains the output of a buffer string, which can be used to create a string.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-stringwriter.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-stringwriter.webp","width":1200,"height":628,"caption":"java stringwriter"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-stringwriter-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java StringWriter Class"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/90033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=90033"}],"version-history":[{"count":3,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/90033\/revisions"}],"predecessor-version":[{"id":447839,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/90033\/revisions\/447839"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447382"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=90033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=90033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=90033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}