您的位置:首页 > 编程语言 > C#

C# txt文件读写

2016-05-10 21:10 489 查看
string 写入txt文件

C#写入文本文件主要用到两个类:System.IO.File、System.IO.StreamWriter,具体使用方法如下:

C# Code
1
2
3
4

5
6

7

8

9

10

11

12

13

14

15

16

17

18

//写入字符数组,文件不存在创建,存在覆盖

string[] lines = { "写入", "多个", "字符串", "到文本" };

System.IO.File.WriteAllLines(@"D:\test.txt", lines, System.Text.Encoding.UTF8);

string str = "写入一个字符串到文本文件";

System.IO.File.WriteAllText(@"D:\test1.txt", str, System.Text.Encoding.UTF8);//写入一个字符串

//以流的形式写入,在using 语句块中使用,保证块结束时 StreamWriter 立即被关闭
//StreamWriter第二个参数可选,为false覆盖文件,为true追加到文件

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\test2.txt", true))

{

foreach (string line in lines)

{

if (!line.Contains("写入"))

{

file.Write(line); //追加,不换行

file.WriteLine(line); // 追加,换行

}

}

}
.NET Framework 在框架的多个领域里使用了流模型。流是允许你用相似的方式(作为顺序字节流)对待不同数据源的一种抽象。所有 .NET 流类从 System.IO.Stream 类继承。一定要记得关闭流,它会释放文件句柄并允许其他人访问文件。

读取txt文件到string

C#写入文本文件主要用到两个类:System.IO.File、System.IO.StreamReader,具体使用方法如下:

C# Code
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

string text = System.IO.File.ReadAllText(@"D:\test.txt");//读取所有行作为一个字符串

Console.WriteLine(text);

string[] lines = System.IO.File.ReadAllLines(@"D:\test1.txt");//按行读取为字符串数组

foreach (string line in lines)

{

Console.WriteLine(line);

}

//以流的方式读出文本文件,using保证了流在使用结束后关闭

using (System.IO.StreamReader sr = new System.IO.StreamReader(@"D:\test.txt"))//按行读取为字符串数组

{

string str;

while ((str = sr.ReadLine()) != null)

{

Console.WriteLine(str);

}

}
在任意时刻都可以调用 Flush()确保所有的数据都写到了磁盘上,因为 StreamWriter 为了优化性能会在内存中缓存你的数据

DataTable写入txt文件

DataTable数据写入到txt文件,可以先把DataTable数据转化成string,再用写入一个字符串的方法写入

C# Code
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

public static string DataTableToString(DataTable dt)//将DataTable数据转化成string

{

string dtstring = "";

for (int i = 0; i < dt.Columns.Count; i++)

{

dtstring = dtstring + dt.Columns[i].ColumnName + "\t";

}

dtstring = dtstring + "\r\n";

for (int i = 0; i < dt.Rows.Count; i++)

{

for (int j = 0; j < dt.Columns.Count; j++)

{

dtstring = dtstring + dt.Rows[i][j] + "\t";

}

dtstring = dtstring + "\r\n";

}

return dtstring;

}
读取txt文件到DataTable

txt文件格式与写入的相同,同时也默认txt文件中第一行为列名。采用按行读取为字符串数组的方式读取txt文件,即:

string[] lines = System.IO.File.ReadAllLines(@"D:\test1.txt");

C# Code
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

public static DataTable StringToDataTable(string[] args)//将字符数组写入到DataTable中

{

String[] columns = args[0].TrimEnd('\n', '\r', '\t').Split('\t');//获取列名,txt文件第一行默认为列名

string[] types = args[1].TrimEnd('\n', '\r', '\t').Split('\t');//获取各列的数据类型

DataTable dt = new DataTable();

for (int i = 0; i < columns.Length; i++)

{

dt.Columns.Add(columns[i],types[i].GetType());//若txt文件第一行为数据,可采用常规方法增加对应的列

}

for (int i = 1; i < args.Length; i++)

{

string[] strs = args[i].TrimEnd('\n', '\r', '\t').Split('\t');//去除字符串末尾的换行符和table符,获取各列的数据

DataRow dr = dt.NewRow();

for (int j = 0; j < columns.Length; j++)

{

dr[j] = strs[j];

}

dt.Rows.Add(dr);

}

return dt;

}
文本文件的读写还可以用System.IO.FileStream类来实现。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: