C # UTF-8 format interchange with java files

Source: Internet
Author: User
Write and install Web projects in Java Program Written in C #, the installation needs to use C # program to read the. Java file, the file encoding format is UTF-8, the first to read and write Java files:
Using (streamreader sr =
New streamreader (New filestream (filepath, filemode. Open ),
System. Text. encoding. utf8 ))
{
Using (streamwriter Sw =
New streamwriter (New filestream (newfile, filemode. Create ),
System. Text. encoding. utf8 ))
{
String line = "";
While (line = Sr. Readline ())! = NULL)
{
For (INT I = 0; I <args. length; I ++)
{
If (line. Contains (ARGs [I])
{
If (! Line. Contains ("=") break;
Int offset = line. indexof ('= ');
String replacedstr = line. substring (Offset + 1 );
Line = line. Replace (replacedstr, "" + values [I] + ";");
}
}
Sw. writeline (line );
}
}
}

The results showed that the generated Java file always contained garbled characters. Later, the research found that:

Use encoding. utf8 to automatically include a three-byte Bom. If you do not want to add a Bom. Utf8encoding utf8 = new utf8encoding should be used instead (whether to add BOM );

The header of the Java file does not contain the BOM of these three bytes. Therefore, the C # source code is changed to the following:

Utf8encoding utf8 = new utf8encoding (false );

Using (streamreader sr =
New streamreader (New filestream (filepath, filemode. Open ),
Utf8 ))
{
Using (streamwriter Sw =
New streamwriter (New filestream (newfile, filemode. Create ),
Utf8 ))
{
String line = "";
While (line = Sr. Readline ())! = NULL)
{
For (INT I = 0; I <args. length; I ++)
{
If (line. Contains (ARGs [I])
{
If (! Line. Contains ("=") break;
Int offset = line. indexof ('= ');
String replacedstr = line. substring (Offset + 1 );
Line = line. Replace (replacedstr, "" + values [I] + ";");
}
}
Sw. writeline (line );
}
Sr. Close ();
Sw. Close ();
}
}

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.