时间:2021-01-27 10:39:30 | 栏目: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>
避免读写中文乱码
总结