0

I have a problem. I am using a file to load some strings to use them in my App.

I have this function:

    public void lecturaFichero(){

    String linea = null;


    try {
            InputStream in = cntx.getAssets().open("cc.txt");

            if (in != null) {
                InputStreamReader input = new InputStreamReader(in,Charset.forName("iso-8859-1"));
                BufferedReader buffreader = new BufferedReader(input);

                while ((linea = buffreader.readLine()) != null) {
                    rellenaCodigo(linea);
                }
                in.close();
            }
    } catch (IOException e) {
     e.printStackTrace();
    }


}

The problem is that when I run the app it crashes right away (reading the file is the first thing I do).

If I do this instead of the above:

InputStreamReader input = new InputStreamReader(in)); //Without specifying the charset

It does work, it does not crash but the app shows where that special characters should be.

I need to solve this, I'd appreciate a solution in which I can read special characters in my app.

Thanks in advance.

PS: Android can print special characters because when I type a String by hand and print it on the screen it shows the character, the problem is when it comes to reading from the .txt.

yafrack
  • 654
  • 1
  • 8
  • 24
  • 1
    What's the exception exactly and did you try some other charset like UTF-8 instead? Also have a look at this SO question: http://stackoverflow.com/questions/499010/java-how-to-determine-the-correct-charset-encoding-of-a-stream - might come in handy. – Darwind Feb 18 '13 at 22:37
  • https://www.dropbox.com/s/82hg9gmli5kbesq/log.txt - I can't use UTF-8 because this charset does not have the characters I need. If I type InputStreamReader input = new InputStreamReader(in,Charset.forName("UTF-8")); the app doesn't crash but shows > instead of that characters. – yafrack Feb 19 '13 at 06:19
  • 1
    Well the content of the file probably isn't charset ISO-8859-1. Did you look at the link I gave you to another SO thread? Also what does the file look like you're trying to read? – Darwind Feb 19 '13 at 16:33
  • I don't know how to check the file's charset but I see those characters fine with my notepad. I've tried to use UTF-8 with codes like á and cleaning the string after the str = str.replaceAll("á","á"); but it doesn't work neither. There's something wrong with accents. – yafrack Feb 19 '13 at 23:04
  • 1
    I tried creating a file and reading it. I set the charset to ISO-8859-1 and created the file with this charset. The char á are read correctly. The file you're reading is clearly created in another charset. Follow the link I supplied you and read the answer for that SO thread. There a libraries out there that can tell you what charset the specific file is. When you know the correct charset, set the `InputStreamReader` charset to this charset. – Darwind Feb 19 '13 at 23:22
  • Actually the file is UTF8 you said. I can't convert it with the iconv command as suggested in the other SO... The text is the one present here https://www.dropbox.com/s/ra1dgvuc2b7c7un/cc.docx but I want it in a txt from which I can read accents. Thanks again for your quick help – yafrack Feb 19 '13 at 23:45
  • I am not able to read it. I have Set the file to ISO-8859-1 in my text editor, I've tried to read it and no way. – yafrack Feb 21 '13 at 20:04
  • 1
    A .docx file is an xml document. You'll need to write a parser for this or use an already existing one like the http://poi.apache.org/. This is my code for reading a txt file set to utf-8 charset: http://pastebin.com/DxxJ22rG - it's working fine. The chars in your file is not ISO-8859-1 format - it can be read with UTF-8. Bear in mind, my code is pure Java, not using any specific Android code. – Darwind Feb 21 '13 at 20:19
  • No no, I am reading a .txt with that content copied. I had my code working with plane Java code since I had the motor of the app in Java (the functionalities) and now it's not working on Android but yeah, it was working with plane Java code. – yafrack Feb 21 '13 at 21:12
  • Okay, something rare but it might help. Now instead of reading from assets I'm reading from raw folder. It just started working, just in case it might help someone. Thanks Darwind – yafrack Feb 21 '13 at 21:21

0 Answers0