文件编码的测试(android)
做了一个文件编码的测试,为下一个作品做准备,需要准备4个不同编码的文件
在 code 中已指明了文件名
xml 代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/btn_utf8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="utf8"
/>
<Button
android:id="@+id/btn_un"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unicode"
/>
<Button
android:id="@+id/btn_ansi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ansi"
/>
<Button
android:id="@+id/btn_bigunicode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bigunicode"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/btn_utf8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="utf8"
/>
<Button
android:id="@+id/btn_un"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unicode"
/>
<Button
android:id="@+id/btn_ansi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ansi"
/>
<Button
android:id="@+id/btn_bigunicode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bigunicode"
/>
</LinearLayout>
java 代码
package zziss.android.txt;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TxttestActivity extends Activity {
/** Called when the activity is first created. */
private Button btn_utf8;
private Button btn_ansi;
private Button btn_unicode;
private Button btn_bigunicode;
private TextView tv;
private String fpath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fpath = "/data/"+Environment.getDataDirectory().getAbsolutePath()+"/zziss.android.txt/";
btn_utf8 = (Button)this.findViewById(R.id.btn_utf8);
btn_ansi = (Button)this.findViewById(R.id.btn_ansi);
btn_unicode = (Button)this.findViewById(R.id.btn_un);
btn_bigunicode = (Button)this.findViewById(R.id.btn_bigunicode);
tv = (TextView)this.findViewById(R.id.tv);
btn_utf8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String fname = fpath + "utf8.txt";
File file = new File(fname);
try {
/*Reader reader = new FileReader(file);
char[] c = new char[100];
reader.read(c);*/
InputStream reader = new FileInputStream(file);
byte[] b = new byte[reader.available()];
reader.read(b);
// 这个也不可以转成 gbk
String s = new String(b,"utf-8");
tv.setText(s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}); // btn_utf8
btn_ansi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String fname = fpath + "ansi.txt";
File file = new File(fname);
try {
/*Reader reader = new FileReader(file);
char[] c = new char[100];
reader.read(c);
String s = String.valueOf(c);*/
InputStream reader = new FileInputStream(file);
byte[] b = new byte[100];
reader.read(b);
// ansi的我一直使用 iso8859-1 或 us-ascii ,但都是乱码,换成 gbk 就 ok 了
String s = new String(b,"GBK");
tv.setText(s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}); // btn_utf8
btn_unicode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String fname = fpath + "unicode.txt";
File file = new File(fname);
try {
/*Reader reader = new FileReader(file);
char[] c = new char[100];
reader.read(c);
String s = String.valueOf(c);*/
InputStream reader = new FileInputStream(file);
byte[] b = new byte[reader.available()];
reader.read(b);
// utf-16 的就不可以转成 gbk
String s = new String(b,"utf-16");
tv.setText(s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}); // unicode
btn_bigunicode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String fname = fpath + "bigunicode.txt";
File file = new File(fname);
try {
/*Reader reader = new FileReader(file);
char[] c = new char[100];
reader.read(c);
String s = String.valueOf(c);*/
InputStream reader = new FileInputStream(file);
byte[] b = new byte[reader.available()];
reader.read(b);
// 对于 bigunicode ,转成 gbk 就可以
String s = new String(b,"GBK");
tv.setText(s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}); // btn_bigunicode
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TxttestActivity extends Activity {
/** Called when the activity is first created. */
private Button btn_utf8;
private Button btn_ansi;
private Button btn_unicode;
private Button btn_bigunicode;
private TextView tv;
private String fpath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fpath = "/data/"+Environment.getDataDirectory().getAbsolutePath()+"/zziss.android.txt/";
btn_utf8 = (Button)this.findViewById(R.id.btn_utf8);
btn_ansi = (Button)this.findViewById(R.id.btn_ansi);
btn_unicode = (Button)this.findViewById(R.id.btn_un);
btn_bigunicode = (Button)this.findViewById(R.id.btn_bigunicode);
tv = (TextView)this.findViewById(R.id.tv);
btn_utf8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String fname = fpath + "utf8.txt";
File file = new File(fname);
try {
/*Reader reader = new FileReader(file);
char[] c = new char[100];
reader.read(c);*/
InputStream reader = new FileInputStream(file);
byte[] b = new byte[reader.available()];
reader.read(b);
// 这个也不可以转成 gbk
String s = new String(b,"utf-8");
tv.setText(s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}); // btn_utf8
btn_ansi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String fname = fpath + "ansi.txt";
File file = new File(fname);
try {
/*Reader reader = new FileReader(file);
char[] c = new char[100];
reader.read(c);
String s = String.valueOf(c);*/
InputStream reader = new FileInputStream(file);
byte[] b = new byte[100];
reader.read(b);
// ansi的我一直使用 iso8859-1 或 us-ascii ,但都是乱码,换成 gbk 就 ok 了
String s = new String(b,"GBK");
tv.setText(s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}); // btn_utf8
btn_unicode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String fname = fpath + "unicode.txt";
File file = new File(fname);
try {
/*Reader reader = new FileReader(file);
char[] c = new char[100];
reader.read(c);
String s = String.valueOf(c);*/
InputStream reader = new FileInputStream(file);
byte[] b = new byte[reader.available()];
reader.read(b);
// utf-16 的就不可以转成 gbk
String s = new String(b,"utf-16");
tv.setText(s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}); // unicode
btn_bigunicode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String fname = fpath + "bigunicode.txt";
File file = new File(fname);
try {
/*Reader reader = new FileReader(file);
char[] c = new char[100];
reader.read(c);
String s = String.valueOf(c);*/
InputStream reader = new FileInputStream(file);
byte[] b = new byte[reader.available()];
reader.read(b);
// 对于 bigunicode ,转成 gbk 就可以
String s = new String(b,"GBK");
tv.setText(s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}); // btn_bigunicode
}
}
分类:
Android
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 上周热点回顾(1.20-1.26)