文件编码的测试(android)
16lz
2021-01-23
做了一个文件编码的测试,为下一个作品做准备,需要准备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软件测试的日志文件抓取简介
- Android 实现沉浸式只需一行代码
- android 文件上传的类--完整 可以直接被调用的
- android学习笔记(4)-android的文件的操作模式与单元测试
- 更改Android模拟器中的hosts文件
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上
- Android使用okhttp框架实现带参数Get和Post请求(附服务端完整代码
- 【Androidd Release】AndroidStudio 发布apk 提示资源文件未翻译