在Android中从UTF-8编码的文本文件中读取阿拉伯语?

时间:2012-08-17 19:41:31

标签: android encoding utf-8 character-encoding

我正在开发一个应用程序,其中我有一些英文和阿拉伯文。为了举例,我可以说它是一个意思是应用的词。这个词是英文的,用户将在阿拉伯语中得到它的含义。

例如:

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

但是当我读到这个本地文件时,我没有按预期获得阿拉伯语。相反,我得到一些奇怪的角色。我确保该文件是UTF-8编码的,当我读取文件时,我再次将编码方案传递给UTF-8 ..但它没有工作。代码段如下:

    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();
    }

感谢任何帮助。谢谢...... !!!

1 个答案:

答案 0 :(得分:0)

我实际上构建了一个处理FileIO的辅助类(并且与希伯来语完全兼容),所以我猜阿拉伯语不会有问题:

 /***
 * 
 * @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;
    }
}

该类的每个实例都处理另一个文件(FILENAME)。

希望这有帮助!