c# 读取txt文件的各种用法(csdn问题)

//行番号 int iLine = 0; //如何在textbox里显示txt文件的内容 string path = @"D:/test.txt";//读取文件txt StringBuilder b = new StringBuilder(); using (FileStream fs = new FileStream(path, FileMode.Open)) { using (StreamReader sr = new StreamReader(fs)) { while (!sr.EndOfStream) { string sLine = sr.ReadLine(); if (sLine.Length < 1) { continue; } string sRecordKbn = sLine.Substring(0,8);//截取的数据 b.Append(sRecordKbn+"/r/n"); } } } TextBox1.Text = b.ToString(); #region/////////////////////////第一种将默认的行终止符(或指定字符串的副本和默认的行终止符)追加到此实例的末尾。 string path = @"D:/test.txt";//读取文件txt string savepath = @"D:/test1.txt";//存放文件txt using (FileStream fs = new FileStream(path, FileMode.Open)) { using (StreamReader sr = new StreamReader(fs)) { while (!sr.EndOfStream) { iLine++; string sLine = sr.ReadLine(); if (sLine.Length < 1) { continue; } string sRecordKbn = sLine.Substring(0, 8);//截取的数据 StringBuilder b = new StringBuilder(); b.AppendLine(sRecordKbn);//将默认的行终止符追加到当前 StringBuilder 对象的末尾。 //另存到其他txt文件中 if (File.Exists(savepath)) { using (StreamWriter sw = File.AppendText(savepath)) { sw.WriteLine(b.ToString()); } } } } } #endregion #region/////////////////////////第一种读取文件然后存放到另外文件里面。 string path = @"D:/test.txt";//读取文件txt string savepath = @"D:/test1.txt";//存放文件txt using (FileStream fs = new FileStream(path, FileMode.Open)) { using (StreamReader sr = new StreamReader(fs)) { while (!sr.EndOfStream) { iLine++; string sLine = sr.ReadLine(); if (sLine.Length < 1) { continue; } if (iLine % 2 == 0) { string sRecordKbn = sLine.Substring(0, 8);//截取的数据 StringBuilder b = new StringBuilder(); b.AppendLine(sRecordKbn); //另存到其他txt文件中 if (File.Exists(savepath)) { using (StreamWriter sw = File.AppendText(savepath)) { sw.WriteLine(b.ToString()); } } } } } } #endregion #region /////////////////////////第二种改正 using (StreamReader sr = new StreamReader(@"D:/test.txt"))//读取文件txt { while (!sr.EndOfStream) { iLine++; string sLine = sr.ReadLine(); if (sLine.Length < 1) { continue; } if (iLine % 2 == 0) { string sRecordKbn = sLine.Substring(0, 8);//截取的数据 if (File.Exists(@"D:/test1.txt"))//提出数据存放文件txt { using (StreamWriter sw = File.AppendText(@"D:/test1.txt")) { sw.WriteLine(sRecordKbn); } } else//文件不存在创建文件 { FileStream fs; fs = File.Create(@"D:/test1.txt");//创建不要用file创建 //使用File.Create创建再复制/移动/删除时会提示:文件正由另一进程使用,因此该进程无法访问该文件 //改用 FileStream 获取 File.Create 返回的 System.IO.FileStream 再进行关闭就无此问题 fs.Close(); using (StreamWriter sw = File.AppendText(@"D:/test1.txt")) { sw.WriteLine(sRecordKbn); } } } } } #endregion

test.txt

11111111:tttttttttttttttttttttt:rrrrrrrrrrrrrrrrrrrrrrrr?yyyyyyyyyyyyyyyyyyyyyyyyyy:ttttttttttt:yyyyyyyyyyyyyyyyyyyy:77777777:88888888:::::::::4444444 22222222:ttttttttttttttttttt:444444444444:%ppppppppp:66666666666666666666666666666666666666666666666666666666666 33333333:444444444444444444444444444444444444444444444444444"55555555555555555555555"^hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh 44444444:444444444444444444444444444444444444444444444444444"55555555555555555555555"^hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh 55555555:tttttttttttttttttttttt:rrrrrrrrrrrrrrrrrrrrrrrr?yyyyyyyyyyyyyyyyyyyyyyyyyy:ttttttttttt:yyyyyyyyyyyyyyyyyyyy:77777777:88888888:::::::::4444444 66666666:ttttttttttttttttttt:444444444444:%ppppppppp:66666666666666666666666666666666666666666666666666666666666 77777777:444444444444444444444444444444444444444444444444444"55555555555555555555555"^hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh 88888888:444444444444444444444444444444444444444444444444444"55555555555555555555555"^hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

 

 

test1.txt

22222222 44444444 22222222 44444444 66666666 88888888 22222222 44444444 66666666 88888888

 

 

 

 

 

 

 

你可能感兴趣的:(String,File,C#,Path,textbox)