namespace 文件的基本操作
{
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99class Program { static void Main(string[] args) { //1.文件的创建 string path = "abc.txt"; if(!File.Exists(path)) { using (File.CreateText(path)) { } } // 2.1 File.AppendText //using (StreamWriter sw = File.AppendText(path)) //{ // sw.WriteLine("中国湖北是我的家!"); //} //2.2 File.AppendAllText // File.AppendAllText(path,"fdsafdsafsa,china"); //2.3 File.AppendAllLines //File.AppendAllLines(path, new string[] { "china", "山海关","黯然销魂掌" }); //2.4 File.Open //using (FileStream fs = File.Open(path, FileMode.Append, FileAccess.Write)) //{ // byte[] buffers = Encoding.UTF8.GetBytes("飞流直下三千尺,疑是银河落九天"+"rn"); // fs.Write(buffers, 0, buffers.Length); //} //2.5 File.OpenWrite //using (FileStream fs = File.OpenWrite(path)) //{ // byte[] buffers = Encoding.UTF8.GetBytes("飞流直下三千尺,疑是银河落九天" + "rn"); // fs.Write(buffers, 0, buffers.Length); //} //2.6 WriteAllBytes // File.WriteAllBytes(path,Encoding.UTF8.GetBytes("云想衣裳花想容,春风扶老厉害了" + "rn")); // File.WriteAllLines(path,new string[]{ "123","china"}); // File.WriteAllText(path,"天上的星星不说话"); //3.1 File.OpenText //using (StreamReader sr = File.OpenText(path)) //{ // Console.WriteLine(sr.ReadToEnd()); //} //3.2 =File.OpenRead //using (FileStream fs =File.OpenRead(path)) //{ // byte[] buffer = new byte[1024]; // int t= fs.Read(buffer,0,1024); // Console.WriteLine(Encoding.UTF8.GetString(buffer,0,t)); //} //3.3 File.Open //using (FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.Read)) //{ // byte[] buffer = new byte[1024]; // int t= fs.Read(buffer,0,1024); // string s = Encoding.UTF8.GetString(buffer,0,t); // Console.WriteLine(s); //} //3.4 File.ReadAllBytes //byte[] buffer= File.ReadAllBytes(path); //string s = Encoding.UTF8.GetString(buffer,0,buffer.Length); //Console.WriteLine(s); //3.5 File.ReadAllLines // string [] strs= File.ReadAllLines(path); //for (int i = 0; i < strs.Length; i++) //{ // Console.Write(strs[i]+"rn"); //} //3.6 File.ReadAllText //string s= File.ReadAllText(path); //Console.WriteLine(s); //3.7 File.ReadLines IEnumerable<string> s= File.ReadLines(path); foreach (string item in s) { Console.WriteLine(item); } Console.ReadKey(); } }
}
最后
以上就是懦弱花生最近收集整理的关于c#基于文件读写的操作的全部内容,更多相关c#基于文件读写内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复