android读写中文如何避免乱码详解

  

  

android读取文件中文出现乱码的原因无非就是,读取文件的字符格式与写如文件的格式不一致。因此,避免中文乱码,要在写入文件的时候按照一定的格式写入,读取的时候按照一定的格式读取。这样对应就不会出现乱码。对于其它的文本读取,在不知道何种格式的时候,可以先读取相应的文件信息,再进行相应的转码。

  

  

<强> RWFile.java

        包com.rwfile.main;      进口java.io.BufferedReader;      进口java.io.BufferedWriter;      进口java.io.File;      进口java.io.FileInputStream;      进口java.io.FileOutputStream;      进口java.io.InputStreamReader;      进口java.io.OutputStreamWriter;      进口android.os.Environment;      公开课RWFile {/* *      *判断sdcard是否存在      *      * @return      */公共静态布尔isSdcard () {      字符串状态=Environment.getExternalStorageState ();      如果(status.equals (Environment.MEDIA_MOUNTED)) {      返回true;      其他}{      返回错误;      }      }/* *      *读取文件内容      *      * @param filePathAndName      * @return      */readFile公共静态字符串(字符串filePathAndName) {      字符串fileContent=零;      尝试{      文件f=新文件(filePathAndName);      如果(f.isFile (),,f.exists ()) {      fileContent=" ";      InputStreamReader阅读=new InputStreamReader (      新FileInputStream (f)、“utf - 8”);      BufferedReader读者=new BufferedReader(阅读);      字符串行;      在((=reader.readLine行())!=null) {      fileContent +=线;      }      read.close ();      }      }捕捉(异常e) {      e.printStackTrace ();      返回null;      }      返回fileContent;      }/* *      *写入文件内容      *      * @param filePathAndName      * @param fileContent      */filePathAndName公共静态布尔writeFile(字符串,字符串fileContent) {      尝试{      文件f=新文件(filePathAndName);      如果(! f.exists ()) {      f.createNewFile ();      }//覆盖文件      OutputStreamWriter写=new OutputStreamWriter (      新的FileOutputStream (f), " utf - 8 ");//覆盖文件//追加文件//OutputStreamWriter写=new OutputStreamWriter (//新FileOutputStream (f,真的),“utf - 8”);//追加文件      BufferedWriter作家=new BufferedWriter(写);      writer.write (fileContent);      writer.close ();      }捕捉(异常e) {      e.printStackTrace ();      返回错误;      }      返回true;      }      }      

根据这个类写的一个测试的演示项目。

  

<强> MainActivity.java

        包com.rwfile.main;      进口java.io.File;      进口android.os.Bundle;   进口android.os.Environment;   进口android.app.Activity;   进口android.view.Menu;   进口android.view.View;   进口android.view.View.OnClickListener;   进口android.widget.Button;   进口android.widget.EditText;   进口android.widget.TextView;   进口android.widget.Toast;      公开课MainActivity延伸活动{      @Override   保护空白>   & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android "      xmlns:工具=" http://schemas.android.com/tools "      android: layout_width=" match_parent "      android: layout_height=" match_parent "      android:取向=按怪薄?      工具:上下文="。祝辞MainActivity”;      & lt; TextView      android: layout_width=" wrap_content "      android: layout_height=" wrap_content "      android:文本=" @string/输入“/比;      & lt; EditText android: id=癅 + id/输入”      android: layout_width=" match_parent "      android: layout_height=" wrap_content "      android:提示=" @string/中国“      android:文本=" @string/中国/比;      & lt;按钮android: id=癅 + id/写”      android: layout_width=" wrap_content "      android: layout_height=" wrap_content "      android:文本=癅string/写/比;      & lt;按钮android: id=癅 + id/阅读”      android: layout_width=" wrap_content "      android: layout_height=" wrap_content "      android:文本=癅string/读/比;      & lt; TextView android: id=癅 + id/内容”      android: layout_width=" wrap_content "      android: layout_height=" wrap_content "/比;      & lt;/LinearLayout>

android读写中文如何避免乱码详解