gpt4 book ai didi

java - 特殊字符 Android (ø)

转载 作者:行者123 更新时间:2023-12-01 04:17:10 25 4
gpt4 key购买 nike

我必须从 txt 文件中检索一些数据,然后在我的应用程序中显示这些数据。

我的问题是,如果我的 txt 中有特殊字符“ø”,则不会显示该字符并显示“?”而是显示。

我尝试检查数据,例如

 if(string.charAt(i) == 'ø') do sth

string.replace('ø' , 'O')

但它们都不起作用,我认为 Java 根本无法识别该字符。

你有什么想法吗?

谢谢

编辑

这就是我读取数据的方式

String[] obj = getText(getActivity(), myTXT.txt").split("\n");

其中 getText 是:

    public String getText(Context c, String fileName){

ByteArrayOutputStream outputStream = null;
try {

AssetManager am = c.getAssets();
InputStream is = am.open(fileName);

outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
while ((len = is.read(buf)) != -1){
outputStream.write(buf,0,len);
}
outputStream.close();
is.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return outputStream.toString();
}

最佳答案

这些字符必须采用 UTF-8 编码,在保存文件时检查文件是否编码。创建一个使用指定编码的构造函数的 InputStreamReader 实例。

InputStreamReader r= new InputStreamReader(new FileInputStream(myFile),"UTF-8");
// read your contents here.
r.close();

关于java - 特殊字符 Android (ø),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19334282/

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