gpt4 book ai didi

android - 如何在 TextView 中显示西类牙语文本?

转载 作者:太空狗 更新时间:2023-10-29 14:30:03 26 4
gpt4 key购买 nike

我有以下代码,我尝试在 TextView 中显示西类牙语文本。当我运行应用程序然后它显示?在一些地方。谁能告诉我显示西类牙语的详细过程。

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

最佳答案

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

关于android - 如何在 TextView 中显示西类牙语文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7735636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com