软件编程
位置:首页>> 软件编程>> C#编程>> C#读写指定编码格式的文本文件

C#读写指定编码格式的文本文件

作者:吴超  发布时间:2023-06-16 03:45:41 

标签:C#,编码格式,文本文件

在工作中经常读写文本文件,在读文件时,需要按开头的两个字节判断文件格式,然后按该格式读文件中的内容。
 写文件时,也要按目标文件指定的格式来写入,只有这样才能让后续的环境正确读入。

1 查看格式
    在vs2010开发环境打开某个文件,然后从菜单上, 文件--高级保存选项,就可看到当前文件的编码格式。
  比如,xx.cs,xx.cshtml文件看到的是[简体中文(GB2312)-代码页936],就是GB2312。
  xx.xml文件看到的是[Unicode(UTF-8带签名)-代码页65001],就是UTF-8。
  常用的格式有:ASCII,UTF-8,UTF-7,UTF-32,Unicode,GB2312 。

2 读格式文件为


Encoding encode=Encoding.GetEncoding("GB2312"));
可以使用后附的类,先读文件的编码格式
encode = fileEncode.GetFileEncodeType("in_file.txt");
string strStr1 = File.ReadAllText("in.txt", encode);

3 写格式文件为


StreamWriter sw = new StreamWriter("out.txt", false, Encoding.GetEncoding("ASCII"));
sw.Write("12.3");
sw.Close();

4 根据文件的编码格式读写文件的完整代码


using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.IO;
using System.Configuration;
using System.Text;

namespace WebApplication1
{
//=1=按文件编码格式读写
 public partial class _Default : System.Web.UI.Page
 {
   string proj_name = "";
   protected void Page_Load(object sender, EventArgs e)
   {
     string xml_in_file="c:\\part.xml";//输入片段,其它程序生成的是一个节点
     string xml_out_file="c:\\all.xml";//整体,新节点片段,要追加到其尾部
//1 读入输入文件的编码格式,并按其编码全部读入文本
     Encoding encode1 = fileEncode.GetFileEncodeType(xml_in_file);

StringBuilder strSb1 = new StringBuilder();
     string strStr1 = File.ReadAllText(xml_in_file, encode1);
//
     StringBuilder strSb = new StringBuilder();
     strSb.Clear();

//2 读入输出文件的编码格式,并按其编码全部读入文本
     Encoding encode6 = fileEncode.GetFileEncodeType(xml_out_file);
     strSb.AppendFormat("{0} \r\n", File.ReadAllText(xml_out_file, encode6));
     strSb.Replace(strStr1, "");//旧的同名段落替换为空
//新节点片段,替换整体末尾标签,即是加入到末尾
     strSb.Replace("</object_set>", strStr1 + "\r\n" + "</object_set>");//新的插入到末尾

// FileInfo myFile = new FileInfo(xml_out_file);
     // StreamWriter sw = myFile.CreateText();
     StreamWriter sw = new StreamWriter(xml_out_file, false, encode6);//Encoding.GetEncoding("GB2312"));
     sw.Write(strSb.ToString());
     sw.Close();
   }
 }

//=2=获得文件编码格式的类
 public class fileEncode
 {//获得文件编码格式的类
   public static System.Text.Encoding GetFileEncodeType(string filename)
   {
     System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
     System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
     Byte[] buffer = br.ReadBytes(2);
     br.Close();
     fs.Close();

if (buffer[0] >= 0xEF)
     {
       if (buffer[0] == 0xEF && buffer[1] == 0xBB)
       {
         return System.Text.Encoding.UTF8;
       }
       else if (buffer[0] == 0xFE && buffer[1] == 0xFF)
       {
         return System.Text.Encoding.BigEndianUnicode;
       }
       else if (buffer[0] == 0xFF && buffer[1] == 0xFE)
       {
         return System.Text.Encoding.Unicode;

}
       else
       {
         return System.Text.Encoding.Default;
       }
     }
     else
     {
       return System.Text.Encoding.Default;
     }
   }

}
}

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com