{"id":2176,"date":"2012-06-11T19:54:25","date_gmt":"2012-06-11T17:54:25","guid":{"rendered":"http:\/\/blog.unelink.es\/?p=2176"},"modified":"2012-06-11T16:55:50","modified_gmt":"2012-06-11T14:55:50","slug":"como-leer-un-buffer-y-poner-su-contenido-en-un-string","status":"publish","type":"post","link":"https:\/\/hosting.airetech.es\/blog\/wiki\/como-leer-un-buffer-y-poner-su-contenido-en-un-string\/","title":{"rendered":"C\u00f3mo leer un b\u00faffer y poner su contenido en un String"},"content":{"rendered":"<p>Como ejemplo, pondremos un m\u00e9todo que recibe el t\u00edpico objeto<\/p>\n<pre>\r\nBufferedReader\r\n<\/pre>\n<p>y nos devuelve otro objeto del tipo<\/p>\n<pre>\r\nString\r\n<\/pre>\n<p>El contenido del m\u00e9todo seria:<\/p>\n<pre>\r\npublic static String leeBuffer(BufferedReader buffer) throws Exception\r\n{\r\n&nbsp;&nbsp;&nbsp;&nbsp;String retorno = null;\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;String lineaSalida = \u201c\u201d;\r\n&nbsp;&nbsp;&nbsp;&nbsp;StringBuffer contenido = new StringBuffer();\r\n&nbsp;&nbsp;&nbsp;&nbsp;String separador = \u201c\u201d;\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;while ((lineaSalida = buffer.readLine()) != null)\r\n&nbsp;&nbsp;&nbsp;&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;contenido.append(separador + lineaSalida);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;separador = \u201c\\n\u201d;\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;retorno = contenido.toString();\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;return retorno;\r\n}\r\n<\/pre>\n<p>El m\u00e9todo anterior podr\u00edamos usarlo, por ejemplo, cuando leemos un resource que podr\u00eda ser un fichero contenido dentro de un fichero jar<\/p>\n<pre>\r\npublic static String leeFichero(Class clase, String localizacion) throws Exception\r\n{\r\n&nbsp;&nbsp;&nbsp;&nbsp;InputStream entrada = clase.getResourceAsStream(localizacion);\r\n&nbsp;&nbsp;&nbsp;&nbsp;BufferedReader buffer = new BufferedReader(new InputStreamReader(entrada));\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;return leeBuffer(buffer);\r\n}\r\n<\/pre>\n<p>o tambi\u00e9n un fichero localizado en nuestro file system<\/p>\n<pre>\r\npublic static final String leeFichero(String ruta) throws Exception\r\n{\r\n&nbsp;&nbsp;&nbsp;&nbsp;File rutaFichero = new File(ruta);\r\n&nbsp;&nbsp;&nbsp;&nbsp;FileReader lectorFichero = new FileReader(rutaFichero);\r\n&nbsp;&nbsp;&nbsp;&nbsp;BufferedReader buffer = new BufferedReader(lectorFichero);\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;return leeBuffer(buffer);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"Como ejemplo, pondremos un m\u00e9todo que recibe el t\u00edpico objeto BufferedReader y nos devuelve otro objeto del tipo String El contenido del m\u00e9todo seria: public static String leeBuffer(BufferedReader buffer) throws Exception { &nbsp;&nbsp;&nbsp;&nbsp;String retorno = null; &nbsp;&nbsp;&nbsp;&nbsp;String lineaSalida = \u201c\u201d; &nbsp;&nbsp;&nbsp;&nbsp;StringBuffer contenido = new StringBuffer(); &nbsp;&nbsp;&nbsp;&nbsp;String separador = \u201c\u201d; &nbsp;&nbsp;&nbsp;&nbsp;while ((lineaSalida = buffer.readLine()) != null) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;contenido.append(separador + lineaSalida); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;separador&nbsp;<a href=\"https:\/\/hosting.airetech.es\/blog\/wiki\/como-leer-un-buffer-y-poner-su-contenido-en-un-string\/\" class=\"read-more\">Seguir leyendo<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[123,6],"tags":[131,124,127],"class_list":["post-2176","post","type-post","status-publish","format-standard","hentry","category-java","category-wiki","tag-buffer","tag-java-2","tag-string","cat-123-id","cat-6-id"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/posts\/2176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/comments?post=2176"}],"version-history":[{"count":10,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/posts\/2176\/revisions"}],"predecessor-version":[{"id":2284,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/posts\/2176\/revisions\/2284"}],"wp:attachment":[{"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/media?parent=2176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/categories?post=2176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/tags?post=2176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}