Code
Private function butClickHandle (e: MouseEvent): void
{
Var file: File = File. Specify topdirectory;
// Open the file
If (e.tar get. id = "but_openfile "){
Var txtfilter: FileFilter = new FileFilter ("Text ","*. as ;*. css ;*. txt ;*. java ;*. html ;*. htm ;*. xml ");
File. browseForOpen ("Open", [txtfilter]);
File. addEventListener (Event. SELECT, function (e: Event): void {
Var file: File = new File(e.tar get. nativePath );
Var filestream: FileStream = new FileStream;
Filestream. open (file, FileMode. READ );
Var bytes: ByteArray = new ByteArray;
Filestream. readBytes (bytes, 0, file. size );
Txtfile. text = transEncodingText (bytes );
});
}
// Open a folder
If (e.tar get. id = "but_opendir "){
File. browseForDirectory ("Open folder ");
File. addEventListener (Event. SELECT, function (e: Event): void {
Txtfile. text = e.tar get. nativePath + File. lineEnding;
Var contents: Array = File(e.tar get). getDirectoryListing ();
For (var I: int = 0; I <contents. length; I ++ ){
Txtfile. text + = contents [I]. name + "\ t" + contents [I]. size + File. lineEnding;
}
});
}
}
// Var bytes: ByteArray = new ByteArray;
// Filestream. readBytes (bytes, 0, file. size );
// Read documents of different codes
Private function transEncodingText (bytes: ByteArray): String
{
// 1. The hexadecimal code at the beginning of the unicode document is ff fe, and the corresponding decimal number is 255,254
If (bytes [0] = 255 & bytes [1] = 254 ){
Return bytes. readMultiByte (bytes. length, "unicode ");
}
// 2. The hexadecimal format starting with unicode big endian is fe ff, and the corresponding decimal number is 254,255.
If (bytes [0] = 254 & bytes [1] = 255 ){
Return bytes. readMultiByte (bytes. length, "UTF-16BE ");
}
// 3. The hexadecimal format starting with UTF-8 is ef bb, and the corresponding decimal number is 239,187
If (bytes [0] = 239 & bytes [1] = 187 ){
Return bytes. readMultiByte (bytes. length, "UTF-8 ");
}
// The system code is used by default to read data.
Return bytes. readMultiByte (bytes. length, File. systemCharset );
}
---------------------------------------------------------------
Refer to other articles:Understanding character encoding