文件内容的写入与读取

 1 //文件的读取方式一
 2 //直接读取文件
 3 
 4 string textContent=File.ReadAllText("路径","读取的文件内容类型");
 5 
 6 //文件读取 方式二
 7 
 8 string FileName=""//表示路径
 9 using(FlieStream fs=new FlieStream('FlieName',FileMode.open,FileAccess.Read))
10 {
11   using(StreamReader sr=new StreamReader(fs))
12     {
13       List<string> strList=new List<String>();
14         while(!sr.EndOfStream)
15         {
16              
17             strList.Add( sr.ReadLine());
18         }  
19     }          
20 }              
21 
22 
23 
24 //文件的写入
25 
26 //方式一 直接写入
27 
28 string txtContent=File.WriteAllText(“文件写入路径”,"写入的内容","内容呈现的格式");
29 
30 //方式二
31 
32 using(FileSteam fs=new FileStream("写入路径",FileMode.OpenOrCreate,FileAccess.ReadWrite))
33 {
34     string content="fsfasf";
35      byte[] strContent=Encoding.Default.GetBytes(content);
36      fs.Write(strContent,0,strContent.length);
37 }
38 
39 
40 //方式三 
41 
42 using(StreamWriter sw=new StreamWriter(“文件写入路径”,"","编码格式","写入的大小"))
43 {
44          sw.WriteLine(“写入的内容”);
45                  sw.Flush();
46 }
47 
48 
49           
View Code

写的不是咋好,希望大家看到有错误的地方指正。

猜你喜欢

转载自www.cnblogs.com/wishbone/p/9112678.html