It simply solves the problem of determining the encoding of TXT before reading ......

Source: Internet
Author: User
// Display the content of the opened file
Void readfile (string path)
{
Try
{
File file = new file (PATH );
Inputstream FS = new fileinputstream (File );
Bufferedreader BR;
// Byte [] buffer = new byte [fs. Available ()];
Byte [] buffer = new byte [3];
FS. Read (buffer );
FS. Close ();

String STR = "";
Int size = (INT) file. Length ();
Int charread = 0;
Char [] content = new char [size];
// Byte [] content = new byte [size];

// Determine the file encoding
If (buffer [0] =-17 & buffer [1] =-69 & buffer [2] =-65)
{
BR = new bufferedreader (New inputstreamreader (
New fileinputstream (file), "UTF-8"); // encoding conversion
While (Br. Ready ())
{
Charread + = Br. Read (content, charread, size-charread );
}
BR. Close ();
STR = new string (content, 0, charread );
Text. settext (STR );
} Else
{
BR = new bufferedreader (New inputstreamreader (
New fileinputstream (file), "GBK"); // encoding conversion
While (Br. Ready ())
{
Charread + = Br. Read (content, charread, size-charread );
}
BR. Close ();
STR = new string (content, 0, charread );
Text. settext (STR );
}

} Catch (ioexception E)
{
System. Out. println ("file reading error ");
}

}

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.