Codenameone 代码名为1的Unicode文件IO

Codenameone 代码名为1的Unicode文件IO,codenameone,Codenameone,我想读取一个Unicode文件(UTF-8)并将其写回另一个文件 我用于阅读的代码是(如中所示) 我甚至试过 text = com.codename1.io.Util.readToString(in,"UTF-8"); 及 但我不是说Unicode不被阅读 因为我在写作 String content = "Some Unicode String"; OutputStream stream = fs.openOutputStream(path + "/" + fileName); stream.

我想读取一个Unicode文件(UTF-8)并将其写回另一个文件

我用于阅读的代码是(如中所示)

我甚至试过

text = com.codename1.io.Util.readToString(in,"UTF-8");

但我不是说Unicode不被阅读

因为我在写作

String content = "Some Unicode String";
OutputStream stream = fs.openOutputStream(path + "/" + fileName);
stream.write(content.getBytes());
stream.close();
试过了,

DataOutputStream dos = new DataOutputStream(stream);
dos.writeUTF(content);
我观察到生成的文件是ANSI编码

更新:解决方案

根据@Shai的回复

阅读:

// For text file in package structure
InputStream in = Display.getInstance().getResourceAsStream(null, "/" + textFile);

// For file in file system
InputStream in = fs.openInputStream(textFile);


if (in != null) {
  try {
      text = com.codename1.io.Util.readToString(in, "UTF-8"); // Encoding
      in.close();
  } catch (IOException ex) {
      text = "Read Error";
  }
}
写:

OutputStream stream = fs.openOutputStream(textFile);
stream.write(content.getBytes("UTF-8"));
stream.close();
readToString()
方法使用UTF-8编码进行读取。如果使用ASCII/ANSI编码之一对文件进行编码,则需要将其修复为UTF-8或指定该方法的特定编码

readUT
F from
DataInputStream
是一种完全不同的东西,专为编码流而不是文本文件设计
DataInputStream
通常不是为Java中的文本文件设计的,您应该使用
Reader
/
InputStreamReader
来处理这类内容


getBytes()
使用特定于平台的编码,这很少是您想要的。您应该使用
getBytes(String)

谢谢您的帮助:)我尝试了所有这些解决方案,但当我尝试在浏览器组件中显示中文时,它看起来像垃圾,我真的认为它可能会起作用。但它仍然没有,我尝试了:out.write(html.getBytes(“UTF-8”);但是HTML本身的编码设置为UTF-8吗?阅读一开始就正常吗?是的,阅读很好,html有utf8的metatag如果这是你的意思,我可以在spanlabel上显示它,中文很好,直到我在ios上的浏览器组件中查看它。设备是否安装了完全中文支持?
// For text file in package structure
InputStream in = Display.getInstance().getResourceAsStream(null, "/" + textFile);

// For file in file system
InputStream in = fs.openInputStream(textFile);


if (in != null) {
  try {
      text = com.codename1.io.Util.readToString(in, "UTF-8"); // Encoding
      in.close();
  } catch (IOException ex) {
      text = "Read Error";
  }
}
OutputStream stream = fs.openOutputStream(textFile);
stream.write(content.getBytes("UTF-8"));
stream.close();