android读写中文如何避免乱码详解
程序员文章站
2022-06-08 17:46:41
前言
android读取文件中文出现乱码的原因无非就是,读取文件的字符格式与写如文件的格式不一致。因此,避免中文乱码,要在写入文件的时候按照一定的格式写入,读取的时候按照...
前言
android读取文件中文出现乱码的原因无非就是,读取文件的字符格式与写如文件的格式不一致。因此,避免中文乱码,要在写入文件的时候按照一定的格式写入,读取的时候按照一定的格式读取。这样对应就不会出现乱码。对于其它的文本读取,在不知道何种格式的时候,可以先读取相应的文件信息,再进行相应的转码。
下面是一个避免中文读写出现乱码的类。
rwfile.java
package com.rwfile.main; import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.inputstreamreader; import java.io.outputstreamwriter; import android.os.environment; public class rwfile { /** * 判断sdcard是否存在 * * @return */ public static boolean issdcard() { string status = environment.getexternalstoragestate(); if (status.equals(environment.media_mounted)) { return true; } else { return false; } } /** * 读取文件内容 * * @param filepathandname * @return */ public static string readfile(string filepathandname) { string filecontent = null; try { file f = new file(filepathandname); if (f.isfile() && f.exists()) { filecontent = ""; inputstreamreader read = new inputstreamreader( new fileinputstream(f), "utf-8"); bufferedreader reader = new bufferedreader(read); string line; while ((line = reader.readline()) != null) { filecontent += line; } read.close(); } } catch (exception e) { e.printstacktrace(); return null; } return filecontent; } /** * 写入文件内容 * * @param filepathandname * @param filecontent */ public static boolean writefile(string filepathandname, string filecontent) { try { file f = new file(filepathandname); if (!f.exists()) { f.createnewfile(); } // 覆盖文件 outputstreamwriter write = new outputstreamwriter( new fileoutputstream(f), "utf-8");// 覆盖文件 // 追加文件 // outputstreamwriter write = new outputstreamwriter( // new fileoutputstream(f, true), "utf-8"); // 追加文件 bufferedwriter writer = new bufferedwriter(write); writer.write(filecontent); writer.close(); } catch (exception e) { e.printstacktrace(); return false; } return true; } }
根据这个类写的一个测试的demo项目。
mainactivity.java
package com.rwfile.main; import java.io.file; import android.os.bundle; import android.os.environment; import android.app.activity; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final edittext input = (edittext) findviewbyid(r.id.input); final textview content = (textview) findviewbyid(r.id.content); button write = (button) findviewbyid(r.id.write); write.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub if (!rwfile.issdcard()) { toast.maketext(mainactivity.this, "无法找到sdcard卡", toast.length_long).show(); } else { string sdcard = environment.getexternalstoragedirectory() .tostring() + file.separator; system.out.println("write path:" + sdcard + "test.txt"); rwfile.writefile(sdcard + "test.txt", input.gettext() .tostring()); } } }); button read = (button) findviewbyid(r.id.read); read.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub if (!rwfile.issdcard()) { toast.maketext(mainactivity.this, "无法找到sdcard卡", toast.length_long).show(); } else { string sdcard = environment.getexternalstoragedirectory() .tostring() + file.separator; system.out.println("read path:" + sdcard + "test.txt"); string str = rwfile.readfile(sdcard + "test.txt"); if (str == null) toast.maketext(mainactivity.this, "无法找到test.txt文件", toast.length_long).show(); else { content.settext(str); } } } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.activity_main, menu); return true; } }
activy_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".mainactivity" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/input" /> <edittext android:id="@+id/input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/chinese" android:text="@string/chinese"/> <button android:id="@+id/write" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/write"/> <button android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/read"/> <textview android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
注意:需要加入文件读写权限
<uses-permission android:name="android.permission.write_external_storage"></uses-permission> <uses-permission android:name="android.permission.mount_unmount_filesystems"></uses-permission>
避免读写中文乱码
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
上一篇: Android通过HTTP协议实现断点续传下载实例
下一篇: 这话好让人想入非非哦