Flex recognizes text file encoding

Source: Internet
Author: User
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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.