前言:
记得前不久,我在公司封闭式开发的日子里,我在宿舍的机子,被同学弄得满身病毒,其中之一是病毒在所有的html里者插入了一段iframe
之后我一不小心..编了段小程序来替换掉所有的iframe,当时忘了文件编码问题..
现在打开才发现一大堆乱码在里面
现在发现了..当然是要解决了:
简单看了一下文件流读出来的字节.做了简单的文件类型编码判断
代码如下:
1 /// <summary>
2 /// 获得文件编码
3 /// </summary>
4 /// <param name="content">文件流的字节数组</param>
5 /// <returns>字符编码</returns>
6 public static Encoding GetFileEncoding(byte[] content)
7 {
8 if (content.Length > 0)
9 {
10 switch (content[0])
11 {
12 case 104:
13 return Encoding.Default;
14 case 255:
15 return Encoding.Unicode;
16 case 254:
17 return Encoding.BigEndianUnicode;
18 case 239:
19 return Encoding.UTF8;
20 default:
21 return Encoding.Default;
22 }
23 }
24 return Encoding.Default;
25 }
2 /// 获得文件编码
3 /// </summary>
4 /// <param name="content">文件流的字节数组</param>
5 /// <returns>字符编码</returns>
6 public static Encoding GetFileEncoding(byte[] content)
7 {
8 if (content.Length > 0)
9 {
10 switch (content[0])
11 {
12 case 104:
13 return Encoding.Default;
14 case 255:
15 return Encoding.Unicode;
16 case 254:
17 return Encoding.BigEndianUnicode;
18 case 239:
19 return Encoding.UTF8;
20 default:
21 return Encoding.Default;
22 }
23 }
24 return Encoding.Default;
25 }
这里只简单做了一下.有更复杂,自己扩展去吧!
反正代码都写到了..再给出两段文件的读和写吧
/// <summary>
/// 读文件流
/// </summary>
/// <param name="stream">文件流;如:File.OpenRead(fileCurrentPath)</param>
/// <param name="encoding">字符编码;如:Encoding.UTF8</param>
/// <returns>流字符串</returns>
public static string ReadFromStream(FileStream stream, Encoding encoding)
{
byte[] content = new byte[stream.Length];
stream.Read(content, 0, content.Length);
stream.Close();
stream = null;
if (encoding == Encoding.Default)
{
encoding = GetFileEncoding(content);
}
return encoding.GetString(content);
}
public static string ReadFromStream(FileStream stream,out Encoding encoding)
{
byte[] content = new byte[stream.Length];
stream.Read(content, 0, content.Length);
stream.Close();
stream = null;
encoding = GetFileEncoding(content);
return encoding.GetString(content);
}
/// 读文件流
/// </summary>
/// <param name="stream">文件流;如:File.OpenRead(fileCurrentPath)</param>
/// <param name="encoding">字符编码;如:Encoding.UTF8</param>
/// <returns>流字符串</returns>
public static string ReadFromStream(FileStream stream, Encoding encoding)
{
byte[] content = new byte[stream.Length];
stream.Read(content, 0, content.Length);
stream.Close();
stream = null;
if (encoding == Encoding.Default)
{
encoding = GetFileEncoding(content);
}
return encoding.GetString(content);
}
public static string ReadFromStream(FileStream stream,out Encoding encoding)
{
byte[] content = new byte[stream.Length];
stream.Read(content, 0, content.Length);
stream.Close();
stream = null;
encoding = GetFileEncoding(content);
return encoding.GetString(content);
}
1 /// <summary>
2 /// 写文件流
3 /// </summary>
4 /// <param name="stream">文件流;如:File.OpenWrite(fileCurrentPath)</param>
5 /// <param name="encoding">字符编码;如:Encoding.UTF8</param>
6 /// <param name="Text">要写的字符串</param>
7 /// <returns>bool</returns>
8 public static bool WriteToStream(FileStream stream, Encoding encoding, string Text)
9 {
10 try
11 {
12
13 byte[] content = encoding.GetBytes(Text.Replace("\n", "\r\n"));
14 stream.SetLength(content.Length);
15 stream.Write(content, 0, content.Length);
16 stream.Close();
17 return true;
18 }
19 catch
20 {
21 return false;
22 }
23 }
2 /// 写文件流
3 /// </summary>
4 /// <param name="stream">文件流;如:File.OpenWrite(fileCurrentPath)</param>
5 /// <param name="encoding">字符编码;如:Encoding.UTF8</param>
6 /// <param name="Text">要写的字符串</param>
7 /// <returns>bool</returns>
8 public static bool WriteToStream(FileStream stream, Encoding encoding, string Text)
9 {
10 try
11 {
12
13 byte[] content = encoding.GetBytes(Text.Replace("\n", "\r\n"));
14 stream.SetLength(content.Length);
15 stream.Write(content, 0, content.Length);
16 stream.Close();
17 return true;
18 }
19 catch
20 {
21 return false;
22 }
23 }
以上代码没有版权,想用拿去用,想改拿去改!
相关文章
暂无评论...