{"id":89841,"date":"2024-11-18T18:00:38","date_gmt":"2024-11-18T12:30:38","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89841"},"modified":"2024-11-18T18:26:24","modified_gmt":"2024-11-18T12:56:24","slug":"java-chararraywriter-class","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/","title":{"rendered":"Java CharArrayWriter Class"},"content":{"rendered":"<p>The Java.io.CharArrayWriter class implements a character buffer that can be used as a Writer. Data written to the stream automatically expands the buffer.<\/p>\n<h2>Explanation<\/h2>\n<p>The CharArrayWriter class is a subclass of the Writer abstract class. Its purpose is to write characters from a char array to a buffer that is also a char array and then from this buffer to a character stream.<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class CharArrayWriter extends Writer<\/pre>\n<h3>Constructor<\/h3>\n<ul>\n<li><strong>CharArrayWriter()<\/strong><br \/>\nWith the given array of characters, a CharArrayReader is created.<\/li>\n<li><strong>CharArrayWriter(int initialSize)<\/strong><br \/>\nUsing the given initial size generates a new CharArrayWriter.<\/li>\n<\/ul>\n<h3>Method<\/h3>\n<ul>\n<li><strong>CharArrayWriter append(char c)<\/strong><br \/>\nThe specified character is appended to this writer by this method.CharArrayWriter append(CharSequence csq)The given character sequence is appended to this writer by this method.<\/li>\n<li><strong>CharArrayWriter append(CharSequence csq, int start, int end)<\/strong><br \/>\nThis method adds a subsequence of the given character sequence to this writer.<\/li>\n<li><strong>void close()<\/strong><br \/>\nThis technique ends the stream.<\/li>\n<li><strong>voidFlush()<\/strong><br \/>\nThis technique clears the stream.<\/li>\n<li><strong>void reset()<\/strong><br \/>\nThis method resets the buffer, allowing you to use it again without wasting the previously allocated buffer.<\/li>\n<li><strong>Int size()<\/strong><br \/>\nThis method returns the buffer&#8217;s current size.<\/li>\n<li><strong>char[] toCharArray()<\/strong><br \/>\nThis method returns a copy of the input data.<\/li>\n<li><strong>String toString()<\/strong><br \/>\nThis technique creates a string out of the input data.<\/li>\n<li><strong>void write(char[] c, int off, int len)<\/strong><br \/>\nWrites characters to the buffer using this method.<\/li>\n<li><strong>void write(int c)<\/strong><br \/>\nWrite a character to the buffer using this method.<\/li>\n<li><strong>void write(String str, int off, int len)<\/strong><br \/>\nUsing this method, a string section is written to the buffer.<\/li>\n<li><strong>void writeTo(Writer out)<\/strong><br \/>\nThe contents of the buffer are written to a different character stream using this method.<\/li>\n<\/ul>\n<h3>Field<\/h3>\n<ul>\n<li><strong>protected char[] buf<\/strong><br \/>\nThis is the buffer used to hold data.<\/li>\n<li><strong>protected int count<\/strong><br \/>\nThis is the buffer&#8217;s character count.<\/li>\n<li><strong>protected Object lock<\/strong><br \/>\nThis is the object that keeps this stream&#8217;s operations in sync.<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>Type<\/strong><\/td>\n<td><strong>Method<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">CharArrayWritter<\/span><\/td>\n<td><span style=\"font-weight: 400;\">append<\/span><span style=\"font-weight: 400;\">(char c)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Adds this writer&#8217;s specified character.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">CharArrayWritter<\/span><\/td>\n<td><span style=\"font-weight: 400;\">append<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">CharSequence<\/span><span style=\"font-weight: 400;\"> csq)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Appends to this writer the designated character sequence.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">CharArrayWritter<\/span><\/td>\n<td><span style=\"font-weight: 400;\">append<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">CharSequence<\/span><span style=\"font-weight: 400;\"> csq, int start, int end)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Attached is a portion of the designated character sequence for this writer.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void<\/span><\/td>\n<td><span style=\"font-weight: 400;\">close()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It shutdown the stream<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void<\/span><\/td>\n<td><span style=\"font-weight: 400;\">flush()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Make the stream run.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Reset()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Resetting the buffer allows you to utilize it once more without wasting the previously allocated space.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">int<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Size()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Gives back the buffer&#8217;s current size.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">char[]<\/span><\/td>\n<td><span style=\"font-weight: 400;\">toCharArray()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Gives back a duplicate of the input data.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">string<\/span><\/td>\n<td><span style=\"font-weight: 400;\">toString()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">converts the data input to a string.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Void<\/span><\/td>\n<td><span style=\"font-weight: 400;\">write<\/span><span style=\"font-weight: 400;\">(char[] c, int off, int len)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It enters the buffer with characters.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void<\/span><\/td>\n<td><span style=\"font-weight: 400;\">write<\/span><span style=\"font-weight: 400;\">(int c)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">A character is written to the buffer.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void<\/span><\/td>\n<td><span style=\"font-weight: 400;\">writeTo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">Writer<\/span><span style=\"font-weight: 400;\"> out)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Writes the buffer&#8217;s contents to a different character stream.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Example<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package studytonight;\r\nimport java.io.CharArrayWriter;\r\nimport java.io.FileWriter;\r\npublic class StudyTonight \r\n{\r\n  public static void main(String args[]) throws Exception\r\n  {\r\n    CharArrayWriter writer=new CharArrayWriter();    \r\n    writer.write(\"Hello Studytonight\");    \r\n    FileWriter fileWriter=new FileWriter(\"E:\\studytonight\\\\output.txt\");   \r\n    writer.writeTo(fileWriter);    \r\n    fileWriter.close();    \r\n    System.out.println(\"Data is written successfully...\");    \r\n  }\r\n}<\/pre>\n<p><strong>Output<\/strong><br \/>\nData is written successfully&#8230;<\/p>\n<h4>Example<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.*;\r\nclass A\r\n{\r\npublic static void main(String... ar)\r\n{\r\nchar[] arr1= new char[]{'j','a','v','a'};\r\nchar[] arr2= new char[]{'p','r','o','g','r',\u2019a\u2019,\u2019m\u2019};\r\n\r\ntry\r\n{\r\nCharArrayWriter caw= new CharArrayWriter(); \r\nSystem.out.println(\"Initial Size of the buffer = \"+ caw.size());\r\n\r\nfor(char ch : arr1) \r\n{\r\ncaw.write(ch);\r\n}\r\n\r\ncaw.write(arr2);  \r\n\r\nSystem.out.println(\"Content of buffer = \"+ caw.toString());\r\nSystem.out.println(\"Size of the buffer = \"+ caw.size());\r\n\r\nFileWriter fw = new FileWriter(\"D:\\\\TextBook2.txt\");\r\ncaw.writeTo(fw);\r\n\r\nfw.flush();\r\ncaw.flush();\r\ncaw.close();\r\n}\t\t \t \r\ncatch(IOException e)\r\n{\r\nSystem.out.println(e);\r\n}\r\n\r\n}\r\n}<\/pre>\n<p><strong>Output <\/strong><br \/>\nInitial Size of the buffer = 0<br \/>\nContent of buffer = javaprogram<br \/>\nSize of the buffer = 13<\/p>\n<h3>Conclusion<\/h3>\n<p>Using the Char array writer class, we can write shared data to several files. This class creates a buffer for writing data. When data is written to the stream, this buffer expands automatically and has no set size.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Java.io.CharArrayWriter class implements a character buffer that can be used as a Writer. Data written to the stream automatically expands the buffer. Explanation The CharArrayWriter class is a subclass of the Writer abstract&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447249,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[5526,334,296,335,5527,299,263,250],"class_list":["post-89841","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-chararraywriter-class","tag-chararraywriter-class-in-java","tag-java","tag-java-chararraywriter-class","tag-java-chararraywriter-class-with-example","tag-java-tutorial","tag-java-tutorial-for-beginners","tag-learn-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java CharArrayWriter Class - TechVidvan<\/title>\n<meta name=\"description\" content=\"The CharArrayWriter class is a subclass of the Writer abstract class. Its purpose is to write characters from a char array to a buffer that is also a char array.\" \/>\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-chararraywriter-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java CharArrayWriter Class - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"The CharArrayWriter class is a subclass of the Writer abstract class. Its purpose is to write characters from a char array to a buffer that is also a char array.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-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=\"2024-11-18T12:30:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-18T12:56:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-chararraywriter.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=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java CharArrayWriter Class - TechVidvan","description":"The CharArrayWriter class is a subclass of the Writer abstract class. Its purpose is to write characters from a char array to a buffer that is also a char array.","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-chararraywriter-class\/","og_locale":"en_US","og_type":"article","og_title":"Java CharArrayWriter Class - TechVidvan","og_description":"The CharArrayWriter class is a subclass of the Writer abstract class. Its purpose is to write characters from a char array to a buffer that is also a char array.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-11-18T12:30:38+00:00","article_modified_time":"2024-11-18T12:56:24+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-chararraywriter.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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java CharArrayWriter Class","datePublished":"2024-11-18T12:30:38+00:00","dateModified":"2024-11-18T12:56:24+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/"},"wordCount":517,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-chararraywriter.webp","keywords":["chararraywriter class","chararraywriter class in java","java","java chararraywriter class","java chararraywriter class with example","Java Tutorial","java tutorial for beginners","Learn Java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/","url":"https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/","name":"Java CharArrayWriter Class - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-chararraywriter.webp","datePublished":"2024-11-18T12:30:38+00:00","dateModified":"2024-11-18T12:56:24+00:00","description":"The CharArrayWriter class is a subclass of the Writer abstract class. Its purpose is to write characters from a char array to a buffer that is also a char array.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-chararraywriter.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-chararraywriter.webp","width":1200,"height":628,"caption":"java chararraywriter"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-chararraywriter-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java CharArrayWriter 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\/89841","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=89841"}],"version-history":[{"count":3,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89841\/revisions"}],"predecessor-version":[{"id":447755,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89841\/revisions\/447755"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447249"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}