1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.IO; 5 using System.Linq; 6 using System.Text; 7 using System.Text.RegularExpressions; 8 using System.Web; 9 using System.Web.UI; 10 using System.Web.UI.WebControls; 11 12 namespace WebApplication1 13 { 14 public partial class WebForm2 : System.Web.UI.Page 15 { 16 //txt路径 17 static string Path = "C:\\wb2.txt"; 18 static string path = "E:\\jrf\\文本2.txt"; 19 protected void Page_Load(object sender, EventArgs e) 20 { 21 ReadTxtContent(Path); 22 Write(path); 23 } 24 /// <summary> 25 /// 读取txt内容 26 /// </summary> 27 /// <param name="Path">文件地址</param> 28 public string ReadTxtContent(string Path) 29 { 30 StreamReader sr = new StreamReader(Path, Encoding.Default); 31 string content; 32 string f=""; 33 while ((content = sr.ReadLine()) != null)//按行输出 34 { 35 f+=content; 36 } 37 //截取{}的内容 再写入txt文件 38 var array = f.Split('{'); 39 f = ""; 40 foreach (var x in array) 41 { 42 if (x.Contains("}")) 43 { 44 string[] n = x.Split('}'); 45 f += "{" + n[0] + "}" + "\r\n"; 46 } 47 } 48 return f; 49 } 50 /// <summary> 51 /// 获取ReadTxtContent返回的内容写入.txt 52 /// </summary> 53 /// <param name="path"></param> 54 public void Write(string path) 55 { 56 //FileMode.Append为不覆盖文件效果.create为覆盖 57 FileStream fs = new FileStream(path, mode: FileMode.Append); 58 StreamWriter sw = new StreamWriter(fs); 59 //开始写入 60 61 sw.Write(ReadTxtContent(Path)); 62 //清空缓冲区 63 sw.Flush(); 64 //关闭 65 sw.Close(); 66 fs.Close(); 67 } 68 } 69 }
完整读写txt 并提取{}里的内容
猜你喜欢
转载自www.cnblogs.com/Aspfengge/p/9377125.html
今日推荐
周排行