web项目用JAVA写,安装程序用C#写成,安装时需要利用C#程序读取.java文件,该文件编码格式是UTF-8,一开始这样来读写JAVA文件的:
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);
}
}
}
结果发现生成的JAVA文件总是含有乱码,后来调研得知:
使用 Encoding.UTF8 是自动带 三 byte 的 BOM,如果要不添加 BOM。应该改用 UTF8Encoding utf8 = new UTF8Encoding(是否添加 BOM);
而JAVA文件的头中不包含这三个字节的BOM,因此C#源码改为如下一切恢复正常:
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);
}
}
}
结果发现生成的JAVA文件总是含有乱码,后来调研得知:
使用 Encoding.UTF8 是自动带 三 byte 的 BOM,如果要不添加 BOM。应该改用 UTF8Encoding utf8 = new UTF8Encoding(是否添加 BOM);
而JAVA文件的头中不包含这三个字节的BOM,因此C#源码改为如下一切恢复正常:
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();
}
}