Tuesday 6 March 2012

Android: write string to a file


FileOutputStream os = new FileOutputStream(fileName);
OutputStreamWriter osw = new OutputStreamWriter(os); 
osw.write(str);
osw.flush();
osw.close();

Do not use
DataOutputStream os = new DataOutputStream(new FileOutputStream(file));
os.writeUTF(str); // this will write a weird char at the beginning of the file

os.writeUTF writes the string with a modified UTF-8 encoding. If the file is opened by WebView as HTML, a weird char shows up at the beginning of the file. 


No comments:

Post a Comment