健康一贴灵,专注医药行业管理信息化

c# 读取及写入BOM 和不带BOM的文本文件


private
void button6_Click_3(object sender, EventArgs e) { string filename = AppDomain.CurrentDomain.BaseDirectory + "test.json"; try { FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); int n = (int)fs.Length; byte[] b = new byte[n]; int r = fs.Read(b, 0, n); fs.Close(); rtxFlow.Text = Encoding.UTF8.GetString(b, 0, n); MessageBox.Show("读取成功", "bom.txt"); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误提示"); } } private void button7_Click_1(object sender, EventArgs e) { string filename = AppDomain.CurrentDomain.BaseDirectory + "sql.xml"; try { FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); int n = (int)fs.Length; byte[] b = new byte[n]; int r = fs.Read(b, 0, n); fs.Close(); rtxFlow.Text = Encoding.UTF8.GetString(b, 0, n); MessageBox.Show("读取成功", "non-bom.txt"); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误提示"); } } private void button8_Click(object sender, EventArgs e) { string filename = AppDomain.CurrentDomain.BaseDirectory + "bom.txt"; try { using (StreamWriter writer = new StreamWriter(filename, false, Encoding.UTF8)) { writer.Write(rtxFlow.Text); } MessageBox.Show("写入成功", "bom.txt"); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误提示"); } } private void button9_Click(object sender, EventArgs e) { try { string filename = AppDomain.CurrentDomain.BaseDirectory + "nob-bom.txt"; using (StreamWriter writer = new StreamWriter(filename, false, new UTF8Encoding(false))) { writer.Write(rtxFlow.Text); } MessageBox.Show("写入成功", "nob-bom.txt"); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误提示"); } }

 

写入的bom.txt ,带有"EF BB BF "标志

 

posted @ 2022-10-20 17:35  一贴灵  阅读(237)  评论(0编辑  收藏  举报
学以致用,效率第一