C#读写指定编码格式的文本文件
作者:吴超 时间:2023-06-16 03:45:41
在工作中经常读写文本文件,在读文件时,需要按开头的两个字节判断文件格式,然后按该格式读文件中的内容。
写文件时,也要按目标文件指定的格式来写入,只有这样才能让后续的环境正确读入。
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;
}
}
}
}
标签:C#,编码格式,文本文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java多线程累加计数的实现方法
2021-07-19 12:10:36
![](https://img.aspxhome.com/file/2023/7/76277_0s.png)
IntelliJ IDEA 好用插件之analyze inspect code详解
2021-09-26 22:16:36
![](https://img.aspxhome.com/file/2023/9/67649_0s.png)
C#判断系统是32位还是64位的方法
2022-04-01 12:44:04
![](https://img.aspxhome.com/file/2023/9/108799_0s.png)
java 静态代理 动态代理深入学习
2022-07-24 21:46:25
Spring Boot 日志配置方法(超详细)
2021-09-06 19:08:52
![](https://img.aspxhome.com/file/2023/8/125228_0s.jpg)
Java如何提供给第三方使用接口方法详解
2022-07-22 19:59:19
基于list stream: reduce的使用实例
2021-07-21 06:54:52
C#实现随机数产生类实例
2021-11-16 15:46:49
Android seekbar实现可拖动进度条
2023-04-02 20:34:45
![](https://img.aspxhome.com/file/2023/8/139338_0s.jpg)
Android游戏开发实践之人物移动地图的平滑滚动处理
2023-12-26 19:45:41
![](https://img.aspxhome.com/file/2023/5/117155_0s.gif)
C#中?、?.、??、??=运算符的用法
2022-11-10 18:11:51
android ListView和GridView拖拽移位实现代码
2023-05-19 18:27:01
![](https://img.aspxhome.com/file/2023/5/100595_0s.png)
详解Spring关于@Resource注入为null解决办法
2023-02-13 18:14:13
![](https://img.aspxhome.com/file/2023/7/82017_0s.png)
Java语言实现Blowfish加密算法完整代码分享
2023-11-02 21:30:07
C# URL短地址压缩算法及短网址原理解析
2023-03-26 08:36:31
SpringBoot ApplicationContext接口深入分析
2021-10-21 23:00:03
![](https://img.aspxhome.com/file/2023/6/61056_0s.png)
Java double转BigDecimal的注意事项说明
2022-12-13 14:44:38
Java爬虫范例之使用Htmlunit爬取学校教务网课程表信息
2021-07-17 00:52:51
![](https://img.aspxhome.com/file/2023/7/88507_0s.png)
Spring boot集成redis lettuce代码实例
2022-08-03 12:21:19
解决RestTemplate第一次请求响应速度较慢的问题
2021-10-20 15:50:50