文件编码

简介:

前言:
      记得前不久,我在公司封闭式开发的日子里,我在宿舍的机子,被同学弄得满身病毒,其中之一是病毒在所有的html里者插入了一段iframe
      之后我一不小心..编了段小程序来替换掉所有的iframe,当时忘了文件编码问题..
      现在打开才发现一大堆乱码在里面

现在发现了..当然是要解决了:
简单看了一下文件流读出来的字节.做了简单的文件类型编码判断
代码如下:

 1 ExpandedBlockStart.gif  /// <summary>
 2InBlock.gif        /// 获得文件编码
 3InBlock.gif        /// </summary>
 4InBlock.gif        /// <param name="content">文件流的字节数组</param>
 5ExpandedBlockEnd.gif        /// <returns>字符编码</returns>

 6 None.gif         public  static Encoding GetFileEncoding( byte[] content)
 7 ExpandedBlockStart.gif         {
 8InBlock.gif            if (content.Length > 0)
 9ExpandedSubBlockStart.gif            {
10InBlock.gif                switch (content[0])
11ExpandedSubBlockStart.gif                {
12InBlock.gif                    case 104:
13InBlock.gif                        return Encoding.Default;
14InBlock.gif                    case 255:
15InBlock.gif                        return Encoding.Unicode;
16InBlock.gif                    case 254:
17InBlock.gif                        return Encoding.BigEndianUnicode;
18InBlock.gif                    case 239:
19InBlock.gif                        return Encoding.UTF8;
20InBlock.gif                    default:
21InBlock.gif                        return Encoding.Default;
22ExpandedSubBlockEnd.gif                }

23ExpandedSubBlockEnd.gif            }

24InBlock.gif            return Encoding.Default;
25ExpandedBlockEnd.gif        }
这里只简单做了一下.有更复杂,自己扩展去吧!

反正代码都写到了..再给出两段文件的读和写吧
ExpandedBlockStart.gif  /// <summary>
InBlock.gif        
/// 读文件流
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="stream">文件流;如:File.OpenRead(fileCurrentPath)</param>
InBlock.gif        
/// <param name="encoding">字符编码;如:Encoding.UTF8</param>
ExpandedBlockEnd.gif        
/// <returns>流字符串</returns>

None.gif         public  static  string ReadFromStream(FileStream stream, Encoding encoding)
ExpandedBlockStart.gif         {
InBlock.gif            byte[] content = new byte[stream.Length];
InBlock.gif            stream.Read(content, 0, content.Length);
InBlock.gif            stream.Close();
InBlock.gif            stream = null;
InBlock.gif            if (encoding == Encoding.Default)
ExpandedSubBlockStart.gif            {
InBlock.gif                encoding = GetFileEncoding(content);
ExpandedSubBlockEnd.gif            }

InBlock.gif            return encoding.GetString(content);
ExpandedBlockEnd.gif        }

None.gif       
None.gif         public  static  string ReadFromStream(FileStream stream, out Encoding encoding)
ExpandedBlockStart.gif         {
InBlock.gif            byte[] content = new byte[stream.Length];
InBlock.gif            stream.Read(content, 0, content.Length);
InBlock.gif            stream.Close();
InBlock.gif            stream = null;
InBlock.gif            encoding = GetFileEncoding(content);
InBlock.gif            return encoding.GetString(content);
ExpandedBlockEnd.gif        }
 1 ExpandedBlockStart.gif    /// <summary>
 2InBlock.gif        /// 写文件流
 3InBlock.gif        /// </summary>
 4InBlock.gif        /// <param name="stream">文件流;如:File.OpenWrite(fileCurrentPath)</param>
 5InBlock.gif        /// <param name="encoding">字符编码;如:Encoding.UTF8</param>
 6InBlock.gif        /// <param name="Text">要写的字符串</param>
 7ExpandedBlockEnd.gif        /// <returns>bool</returns>

 8 None.gif         public  static  bool WriteToStream(FileStream stream, Encoding encoding,  string Text)
 9 ExpandedBlockStart.gif         {
10InBlock.gif            try
11ExpandedSubBlockStart.gif            {
12InBlock.gif
13InBlock.gif                byte[] content = encoding.GetBytes(Text.Replace("\n", "\r\n"));
14InBlock.gif                stream.SetLength(content.Length);
15InBlock.gif                stream.Write(content, 0, content.Length);
16InBlock.gif                stream.Close();
17InBlock.gif                return true;
18ExpandedSubBlockEnd.gif            }

19InBlock.gif            catch
20ExpandedSubBlockStart.gif            {
21InBlock.gif                return false;
22ExpandedSubBlockEnd.gif            }

23ExpandedBlockEnd.gif        }

以上代码没有版权,想用拿去用,想改拿去改!
相关文章
|
7月前
|
存储 数据挖掘 数据处理
【解决 Excel 打开 UTF-8 编码 CSV 文件乱码的 BUG 】
【解决 Excel 打开 UTF-8 编码 CSV 文件乱码的 BUG 】
352 0
|
3月前
|
C#
C# 获取文件编码格式
C# 获取文件编码格式
24 0
|
9月前
|
Java Linux 开发工具
编码 GBK 的不可映射字符 (0x80),sublime设置中文,sublime没有GBK编码选项的问题详解(一)
编码 GBK 的不可映射字符 (0x80),sublime设置中文,sublime没有GBK编码选项的问题详解
290 1
|
9月前
|
存储 Windows
编码 GBK 的不可映射字符 (0x80),sublime设置中文,sublime没有GBK编码选项的问题详解(二)
编码 GBK 的不可映射字符 (0x80),sublime设置中文,sublime没有GBK编码选项的问题详解
192 1
|
文件存储 Python
文本文件的编码格式
文本文件的编码格式
161 0
|
Linux Windows 开发工具
[20180502]UTF8编码问题.txt
[20180502]UTF8编码问题.txt --//上个星期导入执行语句时遇到的问题,做一个记录,开发的脚本使用是UTF8编码,而我们的数据库使用 --//NLS_Lang=AMERICAN_AMERICA.
1067 0
|
JavaScript C# Windows
C#保存文件为无BOM的utf8格式
如图所示,发现用C#的 File.WriteAllLines 方法,无论怎么设置,最终生成的文件都是 PC utf8,也就是CRLF,用SVN进行提交的时候,显示左侧为utf8,右侧为utf8 BOM文件,甚是蛋疼。
2152 0

热门文章

最新文章