A file encoding test is conducted to prepare for the next file. Four files with different codes must be prepared.
The file name is specified in the code.
The xml Code is as follows:
<? 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>
Java code
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 );
// This cannot be converted to 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 I have been using iso8859-1 or us-ascii, but are garbled, changed to 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 can not be converted into 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 );
// Convert bigunicode to 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
}
}