android读写中文如何避免乱码详解
前言
成都创新互联公司长期为上1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为廉江企业提供专业的成都网站建设、做网站,廉江网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。
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
注意:需要加入文件读写权限

避免读写中文乱码
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对创新互联的支持。
分享文章:android读写中文如何避免乱码详解
网页路径:http://www.jxjierui.cn/article/ggisss.html


咨询
建站咨询
