我有以下代码,我尝试用西班牙语在text-view中显示文本。当我运行应用程序然后显示?在某些地方。谁能告诉我显示西班牙语的详细程序。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
textview=(TextView) findViewById(R.id.information);
textview.setText(readTxt());
}
private String readTxt(){
InputStream inputStream = getResources().openRawResource(R.raw.info);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
答案 0 :(得分:1)
您的readTxt方法错误。
您正在返回ByteArrayOutputStream
的字符串表示形式,而不是实际的字符串。
尝试将输入流读入ByteArrayInputStream
,然后从中获取字节数组并返回新的String(byteArray)
;
private String readTxt(){
InputStream inputStream = getResources().openRawResource(R.raw.info);
InputStreamReader isReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(isReader);
StringBuffer buffer = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null)
{
buffer.append(line);
buffer.append("\n");
}
buffer.deleteCharAt(buffer.length() - 1); // Delete the last new line char
// TODO: Don't forget to close all streams and readers
return buffer.toString();
}