我有一个使用 ascii 编码用 C# 编写的文本文件,当我尝试使用 java 项目读取该文件时,我在文件开头得到一个 ZERO WIDTH NO-BREAK SPACE 字符。有人遇到过这种情况吗?
private static void SavePrivateKey(object key)
{
if (logger.IsInfoEnabled) logger.Info("SavePrivateKey - Begin");
string privatekey = (string)key;
string strDirName = Utility.RCTaskDirectory;
string strFileName = "PrivateKey.PPK";
string strKeyPathandName = Path.Combine(strDirName, strFileName);
//if (File.Exists(strKeyPathandName))
//{
// File.Create(strKeyPathandName);
//}
if (!string.IsNullOrEmpty(privatekey))
{//Save private key file
if (!Directory.Exists(strDirName))
Directory.CreateDirectory(strDirName);
FileStream fileStream = new FileStream(strKeyPathandName, FileMode.OpenOrCreate);
//TODO: Save File as ASCII
using (StreamWriter sw = new StreamWriter(fileStream, Encoding.ASCII))
{
if (logger.IsDebugEnabled) logger.DebugFormat("Saving the private key to {0}.", strKeyPathandName);
sw.Write(privatekey);
sw.Close();
if (logger.IsDebugEnabled) logger.DebugFormat("Saved private key to {0}.", strKeyPathandName);
}
}
if (logger.IsInfoEnabled) logger.Info("SavePrivateKey() - End");
}
看起来文本是用 BOM 编写的,这通常是在编写 Unicode 文件时完成的...这个特定字符是 UTF16 文件的 BOM,所以你的 C# 中一定有某些东西将此文件写为 UTF16...
正如其他人所说,它几乎肯定是一个 Unicode 字节顺序标记。如果您查看文件中的实际字节(而不是字符),您可以知道使用哪种编码来写入文件:
UTF-8 -> EF BB BF
UTF-16 BE -> FE FF
UTF-16 LE -> FF FE
是的,这很正常,请参阅维基百科。它是一个可选字符,您只需处理它即可。因此很可能您没有将文件正确写入 ASCII,因为只有当文件编码为 unicode 时才会出现 BOM。
这是一个字节顺序标记,表明它是 UTF-16 编码的文本文件。
显然它不是用真正的 ASCII 写入文件,可能您的代码只是复制字节,尽管它们超出了 ASCII 范围。可以贴一下你的代码吗?