{"id":699,"date":"2024-03-06T17:57:50","date_gmt":"2024-03-06T17:57:50","guid":{"rendered":"https:\/\/javigomez.org\/?p=699"},"modified":"2025-09-29T20:33:15","modified_gmt":"2025-09-29T20:33:15","slug":"u11-08-escritura-de-archivo-sin-tener-que-cerrarlos","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2024\/03\/06\/u11-08-escritura-de-archivo-sin-tener-que-cerrarlos\/","title":{"rendered":"U11-08-Escritura de archivo sin tener que cerrarlos"},"content":{"rendered":"\n<p>El uso conjunto de try-with-resources con BufferedWriter en Java permite escribir texto en un archivo de forma segura y eficiente, gestionando autom\u00e1ticamente el cierre del recurso. <\/p>\n\n\n\n<p><strong>Bloque try-with-resources: <\/strong>Se declara un recurso BufferedWriter llamado writer inicializado con un FileWriter que apunta a &#8220;miArchivo.txt&#8221;. Java garantiza el cierre autom\u00e1tico del recurso writer al finalizar el bloque try, incluso si ocurre una excepci\u00f3n. <\/p>\n\n\n\n<p><strong>Dentro del bloque try<\/strong>: Se define el texto a escribir en textoAEscribir. Se usa writer.write(textoAEscribir) para escribir el texto en el buffer. Se usa writer.newline() para a\u00f1adir un salto de l\u00ednea. <\/p>\n\n\n\n<p><strong>Bloque catch:<\/strong> Se captura cualquier excepci\u00f3n de tipo IOException que pueda surgir durante la escritura en el archivo e imprime su rastro utilizando e.printStackTrace().<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.BufferedWriter;\nimport java.io.FileWriter;\nimport java.io.IOException;\n\npublic class A01008 {\n\n\tpublic static void main(String&#91;] args) {\n\n\t\ttry(BufferedWriter out = new BufferedWriter(new FileWriter(\"src\\\\U11\\\\PRUEBA2.txt\",true))) {\n\t\t\tString linea=\"Sin cerrar el buffer\";\n\t\t\tout.write(linea);\n\t\t\t\n\t\t} catch (IOException e) {\n\t\t\tSystem.out.println(e);\n\t\t}\n\t}\n\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Java U11 08 Escritura sin close\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/TtdnG8yzd9o?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>El uso conjunto de try-with-resources con BufferedWriter en Java permite escribir texto en un archivo de forma segura y eficiente, gestionando autom\u00e1ticamente el cierre del &hellip; <\/p>\n","protected":false},"author":1,"featured_media":700,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[62,203,332],"class_list":["post-699","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-bufferedwriter","tag-java","tag-try"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/comments?post=699"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/699\/revisions"}],"predecessor-version":[{"id":871,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/699\/revisions\/871"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media\/700"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}