I want to read an Unicode file (UTF-8) and write it back to another file.
Code I used for reading is, (As in Textscreen in Codename One, how to read text file?)
final String textFile = "/readme.txt";
String text = "";
InputStream in = Display.getInstance().getResourceAsStream(null, textFile);
if (in != null){
try {
text = com.codename1.io.Util.readToString(in);
in.close();
} catch (IOException ex) {
System.out.println(ex);
text = "Read Error";
}
}
I even tried
text = com.codename1.io.Util.readToString(in,"UTF-8");
and
DataInputStream dis = new DataInputStream(in);
text = com.codename1.io.Util.readUTF(dis);
But I am not Unicode is not getting read.
For writing I am doing,
String content = "Some Unicode String";
OutputStream stream = fs.openOutputStream(path + "/" + fileName);
stream.write(content.getBytes());
stream.close();
and tried,
DataOutputStream dos = new DataOutputStream(stream);
dos.writeUTF(content);
I observed generated file is ANSI encode.
Update: Solution
As per @Shai's reply,
Read:
// 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";
}
}
Write:
OutputStream stream = fs.openOutputStream(textFile);
stream.write(content.getBytes("UTF-8"));
stream.close();