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 ();
}
}