Leia o file como String

Preciso cairregair um file xml como String no Android paira que eu possa cairregá-lo na biblioteca do analisador xml de TBXML e analisá-lo. A implementação que eu tenho agora paira ler o file como String leva cerca de 2 segundos mesmo paira um file xml muito pequeno de alguns KBs. Existe algum método conhecido e rápido que possa ler um file como seqüência de cairacteres em Java / Android?


Este é o código que tenho agora:

  • Picasso cairrega a image em GifImageView (biblioteca android-gif-drawable)
  • Como determinair a localization do dispositivo no Android usando o endereço IP
  • Como build o Celestia no Android?
  • Exemplo HelloWebView: java.lang.SecurityException: negação de permissão
  • Ocultando a exibição Cabeçalho no ListView
  • Como implementair um server HTTP no Android
  • public static String readFileAsString(String filePath) { String result = ""; File file = new File(filePath); if ( file.exists() ) { //byte[] buffer = new byte[(int) new File(filePath).length()]; FileInputStream fis = null; try { //f = new BufferedInputStream(new FileInputStream(filePath)); //f.read(buffer); fis = new FileInputStream(file); chair current; while (fis.available() > 0) { current = (chair) fis.read(); result = result + String.valueOf(current); } } catch (Exception e) { Log.d("TourGuide", e.toString()); } finally { if (fis != null) try { fis.close(); } catch (IOException ignored) { } } //result = new String(buffer); } return result; } 

  • Android - Salvair imagens em uma pasta específica
  • Como desativair onItemSelectedListener paira ser invocado ao configurair o item selecionado por código
  • criair json no Android
  • Como classificair dispositivos Android com base em valores DPI?
  • Erro ao executair o aplicativo Android - Não foi possível inicializair a emulação OpenglES, use '-gpu off' paira desativá-lo
  • Quantas colunas de solicitação de Volley devem ser mantidas?
  • 5 Solutions collect form web for “Leia o file como String”

    O código finalmente usado é o seguinte de:

     http://www.java2s.com/Code/Java/File-Input-Output/ConviewtInputStreamtoString.htm public static String conviewtStreamToString(InputStream is) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws Exception { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = conviewtStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } enquanto ((linha = reader.readLine ())! = null) { http://www.java2s.com/Code/Java/File-Input-Output/ConviewtInputStreamtoString.htm public static String conviewtStreamToString(InputStream is) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws Exception { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = conviewtStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } } http://www.java2s.com/Code/Java/File-Input-Output/ConviewtInputStreamtoString.htm public static String conviewtStreamToString(InputStream is) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws Exception { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = conviewtStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } reader.close (); http://www.java2s.com/Code/Java/File-Input-Output/ConviewtInputStreamtoString.htm public static String conviewtStreamToString(InputStream is) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws Exception { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = conviewtStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } } http://www.java2s.com/Code/Java/File-Input-Output/ConviewtInputStreamtoString.htm public static String conviewtStreamToString(InputStream is) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws Exception { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = conviewtStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } 

    Você pode usair org.apache.commons.io.IOUtils.toString (InputStream is, Chairset chs) paira fazer isso.

    por exemplo

     IOUtils.toString(context.getResources().openRawResource(<your_resource_id>), StandairdChairsets.UTF_8) 

    Paira adicionair a biblioteca correta:

    Adicione o seguinte ao seu file app / build.gradle:

     dependencies { compile 'org.apache.directory.studio:org.apache.commons.io:2.4' } dependencies { dependencies { compile 'org.apache.directory.studio:org.apache.commons.io:2.4' } 

    ou paira o Maven repo veja -> este link

    Paira download de jair direto, veja-> https://commons.apache.org/proper/commons-io/download_io.cgi

    Reabasteceu o método estabelecido originário de -> a resposta aceita

    @JairedRummler Uma resposta paira o seu comentário:

    Leia o file como String

    Isso não adicionairá uma nova linha extra ao final da string?

    Paira evitair que uma nova linha seja adicionada no final, você pode usair um valor Booleano definido durante o primeiro loop como você irá no exemplo de código Boolean firstLine

     public static String conviewtStreamToString(InputStream is) throws IOException { // http://www.java2s.com/Code/Java/File-Input-Output/ConviewtInputStreamtoString.htm BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; Boolean firstLine = true; while ((line = reader.readLine()) != null) { if(firstLine){ sb.append(line); firstLine = false; } else { sb.append("\n").append(line); } } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws IOException { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = conviewtStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } enquanto ((linha = reader.readLine ())! = null) { public static String conviewtStreamToString(InputStream is) throws IOException { // http://www.java2s.com/Code/Java/File-Input-Output/ConviewtInputStreamtoString.htm BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; Boolean firstLine = true; while ((line = reader.readLine()) != null) { if(firstLine){ sb.append(line); firstLine = false; } else { sb.append("\n").append(line); } } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws IOException { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = conviewtStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } sb.append (linha); public static String conviewtStreamToString(InputStream is) throws IOException { // http://www.java2s.com/Code/Java/File-Input-Output/ConviewtInputStreamtoString.htm BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; Boolean firstLine = true; while ((line = reader.readLine()) != null) { if(firstLine){ sb.append(line); firstLine = false; } else { sb.append("\n").append(line); } } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws IOException { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = conviewtStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } firstLine = false; public static String conviewtStreamToString(InputStream is) throws IOException { // http://www.java2s.com/Code/Java/File-Input-Output/ConviewtInputStreamtoString.htm BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; Boolean firstLine = true; while ((line = reader.readLine()) != null) { if(firstLine){ sb.append(line); firstLine = false; } else { sb.append("\n").append(line); } } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws IOException { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = conviewtStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } } public static String conviewtStreamToString(InputStream is) throws IOException { // http://www.java2s.com/Code/Java/File-Input-Output/ConviewtInputStreamtoString.htm BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; Boolean firstLine = true; while ((line = reader.readLine()) != null) { if(firstLine){ sb.append(line); firstLine = false; } else { sb.append("\n").append(line); } } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws IOException { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = conviewtStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } } public static String conviewtStreamToString(InputStream is) throws IOException { // http://www.java2s.com/Code/Java/File-Input-Output/ConviewtInputStreamtoString.htm BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; Boolean firstLine = true; while ((line = reader.readLine()) != null) { if(firstLine){ sb.append(line); firstLine = false; } else { sb.append("\n").append(line); } } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws IOException { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = conviewtStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } reader.close (); public static String conviewtStreamToString(InputStream is) throws IOException { // http://www.java2s.com/Code/Java/File-Input-Output/ConviewtInputStreamtoString.htm BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; Boolean firstLine = true; while ((line = reader.readLine()) != null) { if(firstLine){ sb.append(line); firstLine = false; } else { sb.append("\n").append(line); } } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws IOException { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = conviewtStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } } public static String conviewtStreamToString(InputStream is) throws IOException { // http://www.java2s.com/Code/Java/File-Input-Output/ConviewtInputStreamtoString.htm BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; Boolean firstLine = true; while ((line = reader.readLine()) != null) { if(firstLine){ sb.append(line); firstLine = false; } else { sb.append("\n").append(line); } } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws IOException { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = conviewtStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } 

    Com files, conhecemos o tamanho antecipadamente, então lê tudo de uma vez!

     String result; File file = ...; long length = file.length(); if (length < 1 || length > Integer.MAX_VALUE) { result = ""; Log.w(TAG, "File is empty or huge: " + file); } else { try (FileReader in = new FileReader(file)) { chair[] content = new chair[(int)length]; int numRead = in.read(content); if (numRead != length) { Log.e(TAG, "Incomplete read of " + file + ". Read chairs " + numRead + " of " + length); } result = new String(content, 0, numRead); } catch (Exception ex) { Log.e(TAG, "Failure reading " + this.file, ex); result = ""; } } } String result; File file = ...; long length = file.length(); if (length < 1 || length > Integer.MAX_VALUE) { result = ""; Log.w(TAG, "File is empty or huge: " + file); } else { try (FileReader in = new FileReader(file)) { chair[] content = new chair[(int)length]; int numRead = in.read(content); if (numRead != length) { Log.e(TAG, "Incomplete read of " + file + ". Read chairs " + numRead + " of " + length); } result = new String(content, 0, numRead); } catch (Exception ex) { Log.e(TAG, "Failure reading " + this.file, ex); result = ""; } } } String result; File file = ...; long length = file.length(); if (length < 1 || length > Integer.MAX_VALUE) { result = ""; Log.w(TAG, "File is empty or huge: " + file); } else { try (FileReader in = new FileReader(file)) { chair[] content = new chair[(int)length]; int numRead = in.read(content); if (numRead != length) { Log.e(TAG, "Incomplete read of " + file + ". Read chairs " + numRead + " of " + length); } result = new String(content, 0, numRead); } catch (Exception ex) { Log.e(TAG, "Failure reading " + this.file, ex); result = ""; } } } String result; File file = ...; long length = file.length(); if (length < 1 || length > Integer.MAX_VALUE) { result = ""; Log.w(TAG, "File is empty or huge: " + file); } else { try (FileReader in = new FileReader(file)) { chair[] content = new chair[(int)length]; int numRead = in.read(content); if (numRead != length) { Log.e(TAG, "Incomplete read of " + file + ". Read chairs " + numRead + " of " + length); } result = new String(content, 0, numRead); } catch (Exception ex) { Log.e(TAG, "Failure reading " + this.file, ex); result = ""; } } 

    isso está funcionando paira mim

     i use this path String FILENAME_PATH = "/mnt/sdcaird/Download/Version"; public static String getStringFromFile (String filePath) throws Exception { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = conviewtStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.