-1

i have a txt file on assets folder with content with rare characters, for example with this content:

Hola

Holà holá es un vetélélà Holà holá es un vetélélà Holà holá es un vetélélà Holà holá es un vetélélà Holà holá es un vetélélà

And i'm using this code for read the txt file from assets and store it on a string:

public static String readTxt(Context context, String fileName){
    try {           
        AssetManager am = context.getAssets();
        InputStream is = am.open(fileName);         
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();          
        int i= is.read(); 

        while (i != -1){
            byteArrayOutputStream.write(i);
            i = is.read();
        }
        is.close();                     
        return byteArrayOutputStream.toString();
    } catch (Exception e) {e.printStackTrace();}
    return "";
}

The text is being readed with the correct format (spaces and line jumps are OK), but the rare characters (á à é è... ) are not being readed correctly. I'm reading this:

Hola\r\n\r\nHol� hol� es un vet�l�l� Hol� hol� es un vet�l�l� Hol� hol� es un vet�l�l� Hol� hol� es un vet�l�l� Hol� hol� es un vet�l�l�

What i'm doing bad? how can read that text correctly from ASSETS ?

NullPointerException
  • 36,107
  • 79
  • 222
  • 382

1 Answers1

0

I just have to get the string with byteArrayOutputStream.toString("UTF-8"); and store the txt file with UTF-8 encoding :)

thanks a lot to njzk2

NullPointerException
  • 36,107
  • 79
  • 222
  • 382