我在res / raw保存了一个txt文件(UTF-8格式)。当我尝试在我的应用程序中的TextView中显示它的内容时,我会得到垃圾而不是俄语符号。
这是布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id = "@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="18sp"
android:padding="5dp"
android:text="@string/hello_world" />
</LinearLayout>
我的代码:
package com.example.filereader_sample;
import java.io.DataInputStream;
import java.io.InputStream;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) findViewById(R.id.text);
InputStream file = getResources().openRawResource(R.raw.content);
try {
StringBuffer sBuffer = new StringBuffer();
DataInputStream dataIO = new DataInputStream(file);
String str = null;
while ((str = dataIO.readLine()) != null)
sBuffer.append(str + "\n");
text.setText(sBuffer.toString());
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG);
}
}
}
有没有办法正确显示俄罗斯符号?