2

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

例如:

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

任何帮助表示赞赏..谢谢..!!!

4

1 回答 1

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)。

希望这可以帮助!

于 2012-08-17T19:50:22.880 回答