C#讀寫指定編碼格式的文本文件
在工作中經(jīng)常讀寫文本文件,在讀文件時,需要按開頭的兩個字節(jié)判斷文件格式,然后按該格式讀文件中的內(nèi)容。
寫文件時,也要按目標文件指定的格式來寫入,只有這樣才能讓后續(xù)的環(huán)境正確讀入。
1 查看格式
在vs2010開發(fā)環(huán)境打開某個文件,然后從菜單上, 文件--高級保存選項,就可看到當前文件的編碼格式。
比如,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 根據(jù)文件的編碼格式讀寫文件的完整代碼
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";//輸入片段,其它程序生成的是一個節(jié)點 string xml_out_file="c:\\all.xml";//整體,新節(jié)點片段,要追加到其尾部 //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, "");//舊的同名段落替換為空 //新節(jié)點片段,替換整體末尾標簽,即是加入到末尾 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; } } } }
以上就是C#讀寫指定編碼格式文本文件的方式方法,希望對大家的學習有所幫助。
上一篇:C#使用SqlDataAdapter對象獲取數(shù)據(jù)的方法
欄 目:C#教程
下一篇:在winform下實現(xiàn)左右布局多窗口界面的方法
本文標題:C#讀寫指定編碼格式的文本文件
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6657.html
您可能感興趣的文章
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10C#讀寫INI文件的方法
- 01-10c#讀寫App.config,ConfigurationManager.AppSettings 不生效的解決方法
- 01-10輕松學習C#的讀寫操作
- 01-10舉例說明Java多線程編程中讀寫鎖的使用
- 01-10C# Base 64 編碼/解碼實現(xiàn)代碼
- 01-10c#檢測文本文件編碼的方法
- 01-10Windows系統(tǒng)中C#讀寫ini配置文件的程序代碼示例分享
- 01-10C# FileStream文件讀寫詳解
- 01-10C#實現(xiàn)winform中RichTextBox在指定光標位置插入圖片的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法
- 01-11ajax實現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設置