一、4中写入文本的方式:
//①表示清空 txt
StreamWriter mytxt1 = new StreamWriter("D:\\1清空.txt");
string t1 = "";
mytxt1.Write(t1);
mytxt1.Close();
//②表示向txt写入文本
StreamWriter mytxt2 = new StreamWriter("D:\\2写入覆盖.txt");
string t2 = "已写入覆盖";
mytxt2.Write(t2);
mytxt2.Close();
或
//字符串写入文本,若文件存在则覆盖文件
System.IO.File.WriteAllText(@"C:\Users\Administrator\Desktop\写入新建文本文档.txt", text);
//③表示追加文本
StreamWriter mytxt3 = File.AppendText("D:\\3追加.txt");
string t3 = "追加文字";
mytxt3.Write(t3);
mytxt3.Close();
//④表示追加一行文本并换行
StreamWriter mytxt4 = File.AppendText("D:\\4追加换行.txt");
string t4 = "追加换行";
mytxt4.WriteLine(t4);
mytxt4.Close();
二、读取文本的方式
①将txt文件当成一个对象
StreamReader mytxt2 = new StreamReader("D:\\2写入覆盖.txt");
mytxt2.ReadToEnd//读取所有字符到结尾
mytxt2.ReadLine(); //读取文本中的一行
按行读取内容可通过while(读取行不为空)依次取出。
System.IO.StreamReader file =new System.IO.StreamReader(@"d:\4追加换行.txt");
while ((line = file.ReadLine()) != null)
{System.Console.WriteLine(line);counter++;
}file.Close();
②txt的路径作为一个字符串参数,File对象调用方法的一个参数,通过此方法返回字符串。
// 读取所有文本内容。Read the file as one string.
string text = System.IO.File.ReadAllText(@"C:\Users\Administrator\Desktop\新建文本文档.txt");
下图可知,已读取文本文件所有内容,并赋值给字符串text。
③读取文本内容放入数组中
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Administrator\Desktop\新建文本文档2.txt");
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Administrator\Desktop\新建文本文档2.txt");
下图可知,读取文本文件所有内容并放入lines字符串数组中,共54个item(0-53)(行)。
逐个读取数组中文本,可用for each 语句
StreamWriter mytxt4 = File.AppendText("D:\\4追加换行.txt");
foreach (string line in lines)
{
mytxt4.WriteLine(line);
}
StreamWriter mytxt4 = File.AppendText("D:\\4追加换行.txt");foreach (string line in lines){mytxt4.WriteLine(line);}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp0905
{static class Mytext { static void Main(){//①表示清空 txtStreamWriter mytxt1 = new StreamWriter("D:\\1清空.txt");string t1 = "";mytxt1.Write(t1);mytxt1.Close();//②表示向txt写入文本StreamWriter mytxt2 = new StreamWriter("D:\\2写入覆盖.txt");string t2 = "已写入覆盖";mytxt2.Write(t2);mytxt2.Close();//③表示追加文本StreamWriter mytxt3 = File.AppendText("D:\\3追加.txt");string t3 = "追加文字";mytxt3.Write(t3);mytxt3.Close();//④表示追加一行文本并换行StreamWriter mytxt4 = File.AppendText("D:\\4追加换行.txt");string t4 = "追加换行";mytxt4.WriteLine(t4);mytxt4.Close();System.Console.ReadKey();}}
}
以下为调试代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp0905
{static class Mytext { static void Main(){// The files used here were created in the code example// in How to: Write to a Text File. You can of course substitute// other files of your own.// Example #1// 读取所有文本内容。Read the file as one string.string text = System.IO.File.ReadAllText(@"C:\Users\Administrator\Desktop\新建文本文档.txt");StreamWriter mytxt1 = new StreamWriter("D:\\1清空.txt");// Display the file contents to the console.System.Console.WriteLine("Contents of writeText.txt = {0}", text);//字符串写入文本,若文件存在则覆盖文件System.IO.File.WriteAllText(@"C:\Users\Administrator\Desktop\写入新建文本文档.txt", text);// 按行读取文本,存在字符串数组中。Read the file lines into a string array.string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Administrator\Desktop\新建文本文档2.txt");System.Console.WriteLine("Contents of writeLines2.txt =:");StreamWriter mytxt4 = File.AppendText("D:\\4追加换行.txt");foreach (string line in lines){mytxt4.WriteLine(line);// }//using (System.IO.StreamWriter filew = new System.IO.StreamWriter(@"C:\Users\Administrator\Desktop\写入新建文本文档2.txt"))//{ // foreach (string line in lines)// {// //"\t"水平制表符// Console.WriteLine("\t" +line );// //这个也会覆盖原始文件// filew.WriteLine(line);// }}// Keep the console window open in debug mode.Console.WriteLine("Press any key to exit.");System.Console.ReadKey();}}
}