android讀寫(xiě)中文如何避免亂碼詳解
前言
android讀取文件中文出現(xiàn)亂碼的原因無(wú)非就是,讀取文件的字符格式與寫(xiě)如文件的格式不一致。因此,避免中文亂碼,要在寫(xiě)入文件的時(shí)候按照一定的格式寫(xiě)入,讀取的時(shí)候按照一定的格式讀取。這樣對(duì)應(yīng)就不會(huì)出現(xiàn)亂碼。對(duì)于其它的文本讀取,在不知道何種格式的時(shí)候,可以先讀取相應(yīng)的文件信息,再進(jìn)行相應(yīng)的轉(zhuǎn)碼。
下面是一個(gè)避免中文讀寫(xiě)出現(xiàn)亂碼的類(lèi)。
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;
}
}
/**
* 讀取文件內(nèi)容
*
* @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;
}
/**
* 寫(xiě)入文件內(nèi)容
*
* @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;
}
}
根據(jù)這個(gè)類(lèi)寫(xiě)的一個(gè)測(cè)試的Demo項(xiàng)目。
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, "無(wú)法找到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, "無(wú)法找到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, "無(wú)法找到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>
注意:需要加入文件讀寫(xiě)權(quán)限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

避免讀寫(xiě)中文亂碼
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Android垂直滾動(dòng)控件ScrollView使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android垂直滾動(dòng)控件ScrollView的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android無(wú)需權(quán)限調(diào)起系統(tǒng)相機(jī)
在進(jìn)行一些小型APP的開(kāi)發(fā),或者是對(duì)拍照界面沒(méi)有自定義要求時(shí),我們可以用調(diào)起系統(tǒng)相機(jī)的方式快速完成拍照需求2023-03-03
Android View事件分發(fā)和消費(fèi)源碼簡(jiǎn)單理解
這篇文章主要介紹了Android View事件分發(fā)和消費(fèi)源碼簡(jiǎn)單理解的相關(guān)資料,需要的朋友可以參考下2017-07-07
android監(jiān)聽(tīng)軟鍵盤(pán)的彈出與隱藏的示例代碼
本篇文章主要介紹了android監(jiān)聽(tīng)軟鍵盤(pán)的彈出與隱藏的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸(2)
這篇文章主要為大家詳細(xì)介紹了android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸的第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
總結(jié)Android App內(nèi)存優(yōu)化之圖片優(yōu)化
網(wǎng)上有很多大拿分享的關(guān)于A(yíng)ndroid性能優(yōu)化的文章,主要是通過(guò)各種工具分析,使用合理的技巧優(yōu)化APP的體驗(yàn),提升APP的流暢度,但關(guān)于內(nèi)存優(yōu)化的文章很少有看到。下面是我在實(shí)踐過(guò)程中使用的一些方法,很多都是不太成熟的項(xiàng)目,只是將其作為一種處理方式分享給大家。2016-08-08
Android自定義環(huán)形LoadingView效果
這篇文章主要為大家詳細(xì)介紹了Android自定義環(huán)形LoadingView效果的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

