C#控制台基础 streamreader与streamwriter读取一个txt中的内容写到另外一个txt中

时间:2022-04-25 00:23:34

1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace ConsoleApplication4 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 //1.txt存在的 15 using (StreamReader sReader = new StreamReader(@"1.txt", Encoding.Default)) 16 { 17 //2.txt不存在,没有就新建 18 using (StreamWriter sWriter = new StreamWriter(@"2.txt")) 19 { 20 while(!sReader.EndOfStream) 21 { 22 //如果没有到末尾,,我就读一行写一行 23 sWriter.WriteLine(sReader.ReadLine()); 24 } 25 } 26 } 27 Console.WriteLine("OK"); 28 Console.ReadKey(); 29 } 30 } 31 }

2 读取的txt

C#控制台基础 streamreader与streamwriter读取一个txt中的内容写到另外一个txt中

3 控制台效果