0

我们的示例代码

EditText txtData = (EditText) findViewById(R.id.txtData);
Button btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile);
btnReadSDFile.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        // write on SD card file data in the text box
        try {
            //Read a file from SDCard to TextBox(EditText)
            File myFile = new File("/mnt/sdcard/config_data.med");
            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
            String aDataRow = "";
            String aBuffer = "";
            while ((aDataRow = myReader.readLine()) != null) {
                aBuffer += aDataRow + "\n";
            }
            txtData.setText(aBuffer);
            myReader.close();
            String s = txtData.getText().toString();
            Log.e("txtData",s);
        }catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
});

我们的原始文件内容

Welcome
To
All

我们的日志结果截图

在此处输入图像描述

为什么我们的内容在这里崩溃?我们如何解决它?

4

1 回答 1

2

这应该是编码问题。尝试这个:

FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn, "UTF-8"));

参考不同的编码字符集 编码字符集

于 2012-07-06T10:04:51.377 回答