(window.webpackJsonp=window.webpackJsonp||[]).push([[1529],{1937:function(t,e,a){"use strict";a.r(e);var s=a(31),r=Object(s.a)({},(function(){var t=this,e=t.$createElement,a=t._self._c||e;return a("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[a("h1",{attrs:{id:"bufferedwriter"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#bufferedwriter"}},[t._v("#")]),t._v(" BufferedWriter")]),t._v(" "),a("h2",{attrs:{id:"write-a-line-of-text-to-file"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#write-a-line-of-text-to-file"}},[t._v("#")]),t._v(" Write a line of text to File")]),t._v(" "),a("p",[t._v("This code writes the string to a file. It is important to close the writer, so this is done in a "),a("code",[t._v("finally")]),t._v(" block.")]),t._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[t._v('\n public void writeLineToFile(String str) throws IOException {\n File file = new File("file.txt");\n BufferedWriter bw = null;\n try {\n bw = new BufferedWriter(new FileWriter(file));\n bw.write(str);\n } finally {\n if (bw != null) {\n bw.close();\n }\n }\n }\n\n')])])]),a("p",[t._v("Also note that "),a("code",[t._v("write(String s)")]),t._v(" does not place newline character after string has been written. To put it use "),a("code",[t._v("newLine()")]),t._v(" method.")]),t._v(" "),a("p",[t._v("Java 7 adds the "),a("a",{attrs:{href:"https://docs.oracle.com/javase/7/docs/api/java/nio/file/package-summary.html",target:"_blank",rel:"noopener noreferrer"}},[a("code",[t._v("java.nio.file")]),a("OutboundLink")],1),t._v(" package, and "),a("a",{attrs:{href:"http://stackoverflow.com/documentation/java/89/exceptions/1581/using-try-with-resources#t=201607231444506229575",target:"_blank",rel:"noopener noreferrer"}},[t._v("try-with-resources"),a("OutboundLink")],1),t._v(":")]),t._v(" "),a("div",{staticClass:"language-java extra-class"},[a("pre",{pre:!0,attrs:{class:"language-java"}},[a("code",[a("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("void")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token function"}},[t._v("writeLineToFile")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),a("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" str"),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("throws")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("IOException")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),a("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Path")]),t._v(" path "),a("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Paths")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),a("span",{pre:!0,attrs:{class:"token function"}},[t._v("get")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),a("span",{pre:!0,attrs:{class:"token string"}},[t._v('"file.txt"')]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),a("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("try")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),a("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("BufferedWriter")]),t._v(" bw "),a("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Files")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),a("span",{pre:!0,attrs:{class:"token function"}},[t._v("newBufferedWriter")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("path"),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n bw"),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),a("span",{pre:!0,attrs:{class:"token function"}},[t._v("write")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("str"),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),a("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n")])])]),a("h4",{attrs:{id:"syntax"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#syntax"}},[t._v("#")]),t._v(" Syntax")]),t._v(" "),a("ul",[a("li",[t._v("new BufferedWriter(Writer); //The default constructor")]),t._v(" "),a("li",[t._v("BufferedWriter.write(int c); //Writes a single character")]),t._v(" "),a("li",[t._v("BufferedWriter.write(String str); //Writes a string")]),t._v(" "),a("li",[t._v("BufferedWriter.newLine(); //Writes a line separator")]),t._v(" "),a("li",[t._v("BufferedWriter.close(); //Closes the BufferedWriter")])]),t._v(" "),a("h4",{attrs:{id:"remarks"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#remarks"}},[t._v("#")]),t._v(" Remarks")]),t._v(" "),a("ul",[a("li",[t._v("If you try to write from a "),a("code",[t._v("BufferedWriter")]),t._v(" (using "),a("code",[t._v("BufferedWriter.write()")]),t._v(") after closing the "),a("code",[t._v("BufferedWriter")]),t._v(" (using "),a("code",[t._v("BufferedWriter.close()")]),t._v("), it will throw an "),a("code",[t._v("IOException")]),t._v(".")]),t._v(" "),a("li",[t._v("The "),a("code",[t._v("BufferedWriter(Writer)")]),t._v(" constructor does NOT throw an "),a("code",[t._v("IOException")]),t._v(". However, the "),a("code",[t._v("FileWriter(File)")]),t._v(" constructor throws a "),a("code",[t._v("FileNotFoundException")]),t._v(", which extends "),a("code",[t._v("IOException")]),t._v(". So catching "),a("code",[t._v("IOException")]),t._v(" will also catch "),a("code",[t._v("FileNotFoundException")]),t._v(", there is never a need for a second catch statement unless you plan on doing something different with the "),a("code",[t._v("FileNotFoundException")]),t._v(".")])])])}),[],!1,null,null,null);e.default=r.exports}}]);