2

I am working on an application in which I have a some text in English and Arabic. For the sake of example I can say it as a words meaning application. The word is in English and user will get it's meaning in Arabic.

For Example:

    Test     اختبار    // Test is the word and then there is it's meaning in Arabic

But when I read this local file I don't get Arabic as intended. Instead I get some strange characters. I am making sure that file is UTF-8 encoded and when I read the file I again pass encoding scheme to be UTF-8..but it does not wwork. Code snippet is as follows:

    InputStream inputStream = resources.openRawResource(R.raw.textfile);
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));

    try {
        String line;
        while((line = reader.readLine()) != null) {
            String[] strings = TextUtils.split(line, " ");
            if (strings.length < 2) continue;
            addWord(strings[0].trim(), strings[1].trim());
        }
    } finally {
        reader.close();
    }

Any help is appreciated..Thanks..!!!

Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80
  • "it does not work" is too vague to help you diagnose what's wrong. What happens? – Jon Skeet Aug 17 '12 at 19:43
  • possible duplicate of [How to support Arabic text in Android?](http://stackoverflow.com/questions/7962704/how-to-support-arabic-text-in-android) – kabuko Aug 17 '12 at 19:45
  • Are you sure its not supposed to be `"UTF-8"`, it looks like you're using `"utf-8"`. – slayton Aug 17 '12 at 19:52

1 Answers1

0

I actually built an helper class that handles FileIO (and is completely compatible with Hebrew) so I guess Arabic will be no problem:

 /***
 * 
 * @author Android Joker ©
 *  Do NOT copy without confirmation!
 *  Thanks!
 *
 */
public class FileMethods {
    private Boolean isOk;
    private Context mContext;
    private String fileName;
    public FileMethods(Context c, String FILENAME) {
        this.isOk = true;
        this.mContext = c;
        this.fileName = FILENAME;
    }
    public void reWrite(Object DATA) { 
        //For deleting the content of the file and then writing
        try {
            FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
            fos.write(DATA.toString().getBytes());
            fos.close();
            Log.i("File Writing ("+this.fileName+")", "Success!");
            isOk = true;
        }
        catch (IOException e) {
            e.printStackTrace();
            Log.e("File Writing ("+this.fileName+")", "Failed!");
            isOk = false;
        }
    }
    public void Write(Object DATA) {
        //For keeping the previous contents and continue writing
        String data = Read("") + DATA.toString() + "\n";
        try {
            FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
            fos.write(data.getBytes());
            fos.close();
            Log.i("File Writing ("+this.fileName+")", "Success!");
            isOk = true;
        }
        catch (IOException e) {
            e.printStackTrace();
            Log.e("File Writing ("+this.fileName+")", "Failed!");
            isOk = false;
        }

    }
    public void Clear() {
        //For deleting all the file contents
        try {
            FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
            fos.write("".getBytes());
            fos.close();
            Log.i("Cleared"+"("+this.fileName+")", "Success!");
            isOk = true;
        }
        catch (IOException e) {
            e.printStackTrace();
            Log.e("Cleared"+"("+this.fileName+")", "Failed!");
            isOk = false;
        }
    }
    public String Read(String inCaseOfFailure) {
        //For reading (If reading failed for any reason, inCaseOfFailure will be written)
        String info = "";
        try {
            FileInputStream fis = mContext.openFileInput(this.fileName);
            byte[] dataArray = new byte[fis.available()];
            if (dataArray.length>0) {
                while(fis.read(dataArray)!=-1)
                {
                    info = new String(dataArray);
                }
                fis.close();
                Log.i("File Reading ("+this.fileName+")","Success!");
                isOk = true;
            }
            else {
                try {
                    FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
                    fos.write(inCaseOfFailure.getBytes());
                    fos.close();
                    Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Success!");
                    isOk = true;
                }
                catch (Exception e) {
                    e.printStackTrace();
                    isOk = false;
                    Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Failed!");
                    Log.e("File Writing In Case Of Failure ("+this.fileName+")", "MOVING ON");
                }
            }
        }
        catch (FileNotFoundException e) {
            try {
                FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
                if (inCaseOfFailure != null) {
                    fos.write(inCaseOfFailure.getBytes());
                    fos.close();
                    Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Success!");
                    isOk = true;
                }
                else {
                    Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Failed!");
                    isOk = false;
                }
            }
            catch (IOException e1) {
                e.printStackTrace();
                Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Failed!");
                isOk = false;
            }
        }
        catch (IOException e) {
            e.printStackTrace();
            Log.e("File Reading ("+this.fileName+")", "Failed!");
            isOk = false;
        }
        return info;
    }

    public Boolean GetIsOK() {
        //Method that checks whether the FileIO was successfully running or not
        Boolean temp = isOk;
        isOk = true;
        return temp;
    }
}

Each instance of the class handles another file (FILENAME).

Hope this helps!

RE6
  • 2,684
  • 4
  • 31
  • 57