废了好半天的工夫,终究解决android写入txt文件中文乱码的问题。
不多说了,公布代码吧,分享给大家。
public static boolean writeToTxt(Context ctx, String fileName, String content) {
try {
OutputStreamWriter write = null;
BufferedWriter out = null;
if (fileName != null) {
try { // new FileOutputStream(fileName, true) 第2个参数表示追加写入
write = new OutputStreamWriter(new FileOutputStream(
fileName),Charset.forName("gbk"));//1定要使用gbk格式
out = new BufferedWriter(write, BUFFER_SIZE);
} catch (Exception e) {
}
}
out.write(content);
out.flush();
out.close();
return true;
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
return false;
}
}