文件编码的测试(android)
16lz
2022-05-18
做了一个文件编码的测试,为下一个作品做准备,需要准备4个不同编码的文件
在 code 中已指明了文件名
xml 代码如下
<? xmlversion="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 >
java 代码
packagezziss.android.txt;importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.Reader;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.os.Environment;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.TextView;
public classTxttestActivity extendsActivity{
/** Calledwhentheactivityisfirstcreated. */
privateButtonbtn_utf8;
privateButtonbtn_ansi;
privateButtonbtn_unicode;
privateButtonbtn_bigunicode;
privateTextViewtv;
privateStringfpath;
@Override
public voidonCreate(BundlesavedInstanceState){
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( newView.OnClickListener(){
@Override
public voidonClick(Viewv){
// TODOAuto-generatedmethodstub
Stringfname=fpath+"utf8.txt";
Filefile= newFile(fname);
try{
/* Readerreader=newFileReader(file);
char[]c=newchar[100];
reader.read(c); */
InputStreamreader= newFileInputStream(file);
byte[]b= new byte[reader.available()];
reader.read(b);
// 这个也不可以转成gbk
Strings= newString(b,"utf-8");
tv.setText(s);
} catch(FileNotFoundExceptione){
// TODOAuto-generatedcatchblock
e.printStackTrace();
} catch(IOExceptione){
e.printStackTrace();
}
}
}); // btn_utf8
btn_ansi.setOnClickListener( newView.OnClickListener(){
@Override
public voidonClick(Viewv){
// TODOAuto-generatedmethodstub
Stringfname=fpath+"ansi.txt";
Filefile= newFile(fname);
try{
/* Readerreader=newFileReader(file);
char[]c=newchar[100];
reader.read(c);
Strings=String.valueOf(c); */
InputStreamreader= newFileInputStream(file);
byte[]b= new byte[100];
reader.read(b);
// ansi的我一直使用iso8859-1或us-ascii,但都是乱码,换成gbk就ok了
Strings= newString(b,"GBK");
tv.setText(s);
} catch(FileNotFoundExceptione){
// TODOAuto-generatedcatchblock
e.printStackTrace();
} catch(IOExceptione){
e.printStackTrace();
}
}
}); // btn_utf8
btn_unicode.setOnClickListener( newView.OnClickListener(){
@Override
public voidonClick(Viewv){
// TODOAuto-generatedmethodstub
Stringfname=fpath+"unicode.txt";
Filefile= newFile(fname);
try{
/* Readerreader=newFileReader(file);
char[]c=newchar[100];
reader.read(c);
Strings=String.valueOf(c); */
InputStreamreader= newFileInputStream(file);
byte[]b= new byte[reader.available()];
reader.read(b);
// utf-16的就不可以转成gbk
Strings= newString(b,"utf-16");
tv.setText(s);
} catch(FileNotFoundExceptione){
// TODOAuto-generatedcatchblock
e.printStackTrace();
} catch(IOExceptione){
e.printStackTrace();
}
}
}); // unicode
btn_bigunicode.setOnClickListener( newView.OnClickListener(){
@Override
public voidonClick(Viewv){
// TODOAuto-generatedmethodstub
Stringfname=fpath+"bigunicode.txt";
Filefile= newFile(fname);
try{
/* Readerreader=newFileReader(file);
char[]c=newchar[100];
reader.read(c);
Strings=String.valueOf(c); */
InputStreamreader= newFileInputStream(file);
byte[]b= new byte[reader.available()];
reader.read(b);
// 对于bigunicode,转成gbk就可以
Strings= newString(b,"GBK");
tv.setText(s);
} catch(FileNotFoundExceptione){
// TODOAuto-generatedcatchblock
e.printStackTrace();
} catch(IOExceptione){
e.printStackTrace();
}
}
}); // btn_bigunicode
}
}
更多相关文章
- android Java BASE64编码和解码二:图片的编码和解码
- Android开发者指南(12) ―― Android(安卓)Supported Media Form
- Android屏幕禁止休眠的方法
- Android开发实践 带你理解使用WebView
- android onTouchEvent和setOnTouchListener中onTouch的区别
- msm8909编译环境搭建
- android实现获取有线和无线Ip地址的方法
- Android(安卓)- SQLite in Android
- Android(安卓)实现沉浸式只需一行代码