做了一个文件编码的测试,为下一个作品做准备,需要准备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

}
}

更多相关文章

  1. android Java BASE64编码和解码二:图片的编码和解码
  2. Android开发者指南(12) ―― Android(安卓)Supported Media Form
  3. Android屏幕禁止休眠的方法
  4. Android开发实践 带你理解使用WebView
  5. android onTouchEvent和setOnTouchListener中onTouch的区别
  6. msm8909编译环境搭建
  7. android实现获取有线和无线Ip地址的方法
  8. Android(安卓)- SQLite in Android
  9. Android(安卓)实现沉浸式只需一行代码

随机推荐

  1. Android(安卓)Looper(cpp)用法举例
  2. Android CoordinatorLayout + AppBarLayo
  3. android中的左右滑动
  4. android 界面更新之runOnUiThread()
  5. 学习Android Studio开发工具之Activity3(
  6. Mono For Android(安卓)重要的类文件
  7. Android数据存储(一)
  8. Android—— 4.2 Vold挂载管理_NetlinkMa
  9. Android C++层内存泄漏检测
  10. NDK开发笔记——Android使用openssl