一、StreamWriter
可以简化文本类型的Stream的处理
StreamWriter是辅助Stream进行处理的
using (StreamWriter writer = new StreamWriter(stream, encoding))
{
writer.WriteLine("你好");
}
StreamWriter对Stream进行了包装,调一下writer.WriteLine背后帮我们做了把字符串转换为二进制的工作。
练习:把一个字符串写入到某个txt文档中
1 string str = "脸乃身外之物,可要可不要,钱乃必要之物,不得不要。"; 2 //using 可以带释放,如果不使用,要手动关闭 3 using (FileStream fs = File.OpenWrite("d:\\011.txt")) 4 { 5 using (StreamWriter sw = new StreamWriter(fs)) //可以指定编码 StreamWriter sw = new StreamWriter(fs,Encoding.UTF8) 6 { 7 sw.Write(str); 8 } 9 10 }
二、StreamReader
和StreamWriter类似, StreamReader简化了文本类型的流的读取。
1 Stream stream = File.OpenRead("d:\\3.txt"); 2 3 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) 4 { 5 //Console.WriteLine(reader.ReadToEnd()); 6 Console.WriteLine(reader.ReadLine()); 7 8 }
ReadToEnd用于从当前位置一直读到最后,内容大的话会占内存;每次调用都往下走,不能无意中调用了两次
ReadLine读取一行,如果到了末尾,则返回null。
练习:打开一个文件,把里面员工的工资全部加1000,然后生成一个修改好的文件(必须理解哦)
1(理解版)
1 /* 2 File.OpenRead(path); File.OpenWrite(path); 3 案例:对职工工资文件处理,所有人的工资加倍然后输出到新文件。 4 ----新手写代码为了好理解就不加using了----------- 5 */ 6 7 //打开现有文件把读取到FileStream流fs里面 8 FileStream fs = File.OpenRead("d:\\13.txt"); 9 //fs.Read()操作的是文件流,不方便单行读取(无法确定单行有多少字节) 10 //实例化一个StreamReader流sr来接收并指定编码(用指定的字符编码为指定的流初始化) 11 StreamReader sr = new StreamReader(fs, Encoding.Default); 12 //打开现有文件进行写入 13 FileStream fs2 = File.OpenWrite("d:\\1111.txt"); 14 //给我个打开的文件,我要写入指定的编码 O(∩_∩)O哈哈~..终于他妈的理解了 15 StreamWriter sw = new StreamWriter(fs2, Encoding.Default); 16 17 string n; 18 //只要sr.ReadLine()不为null就一直调用(注意:调用一次,第二次调用它就读下一行了,依次。。) 19 while ((n = sr.ReadLine()) != null) 20 { 21 //用一个字符串数组接收n的分割字符串(string 小米渣,string 3000) 22 string[] strs = n.Split(',', ','); 23 //把工资进行转换并加上1000 24 int salary = Convert.ToInt32(strs[1]) + 1000; 25 //掉用sw.WriteLine把处理后的字符串进行写入文本流 26 sw.WriteLine(strs[0] + "," + salary); 27 28 } 29 //用手动关闭,txt文件中没有内容哦! 30 sw.Close(); 31 Console.ReadKey(); 32 }
2(纯纯版)
1 using (FileStream fs = File.OpenRead("d:\\13.txt")) 2 { 3 using (StreamReader sr = new StreamReader(fs, Encoding.Default)) 4 { 5 using (FileStream fs2 = File.OpenWrite("d:\\1111.txt")) 6 { 7 using (StreamWriter sw = new StreamWriter(fs2, Encoding.Default)) 8 { 9 string n; 10 while ((n = sr.ReadLine()) != null) 11 { 12 string[] strs = n.Split(',', ','); 13 int salary = Convert.ToInt32(strs[1]) + 1000; 14 sw.WriteLine(strs[0] + "," + salary); 15 } 16 } 17 } 18 } 19 }
3(理解版+纯纯版的组合版)
1 Stopwatch watch = new Stopwatch(); 2 watch.Start(); 3 FileStream fs = File.OpenRead("d:\\13.txt"); 4 StreamReader sr = new StreamReader(fs, Encoding.Default); 5 FileStream fs2 = File.OpenWrite("d:\\1111.txt"); 6 //就写入的时候用了using 7 using (StreamWriter sw = new StreamWriter(fs2, Encoding.Default)) 8 { 9 string n; 10 while ((n = sr.ReadLine()) != null) 11 { 12 string[] strs = n.Split(',', ','); 13 int salary = Convert.ToInt32(strs[1]) + 1000; 14 sw.WriteLine(strs[0] + "," + salary); 15 } 16 } 17 watch.Stop(); 18 Console.WriteLine(watch.Elapsed); 19 20 Console.ReadKey();
4、标准版
1 Stopwatch watch = new Stopwatch(); 2 watch.Start(); 3 //标准版开始 4 //定义一个集合来存储读取的内容 5 List<string> li = new List<string>(); 6 using (FileStream fs = File.OpenRead("d:\\13.txt")) 7 { 8 using (StreamReader sr = new StreamReader(fs, Encoding.Default)) 9 { 10 string n; 11 while ((n = sr.ReadLine()) != null) 12 { 13 string[] strs = n.Split(',', ','); 14 int salary = Convert.ToInt32(strs[1]) + 1000; 15 li.Add(strs[0] + "," + salary); 16 } 17 } 18 } 19 20 using (FileStream fs2 = File.OpenWrite("d:\\1111.txt")) 21 { 22 using (StreamWriter sw = new StreamWriter(fs2, Encoding.Default)) 23 { 24 //遍历集合并写入文件 25 foreach (string item in li) 26 { 27 sw.WriteLine(item); 28 } 29 } 30 } 31 //标准版结束 32 watch.Stop(); 33 Console.WriteLine(watch.Elapsed);
★★结论:我用Stopwatch测试了下 用(纯纯版)版运行时间0.0006875 (理解版)0.0008121 (理解版+纯纯版的组合版) 0.0006101
using 要如何用?是不是只要用在写入时候就可以了?这样代码能少很多,这样做好吗??--- 后面再回答喽,O__O"…
看了这位前辈的《使用using关键字自动释放资源未必一定就会有明显好处》,还是不懂,如果有大侠看到能不能给个回复呢?谢谢了哈?
三、WebClient
练习:读取百度首页的内容
1 static void Main(string[] args) 2 { 3 //初始化WebClient类 4 WebClient wc = new WebClient(); 5 //定义一个Stream变量 st接收wc.OpenRead()的返回值 6 using (Stream st = wc.OpenRead("http://www.baidu.com")) 7 { 8 //创建一个StreamReader实例,并指定编码 9 using (StreamReader sr = new StreamReader(st, Encoding.UTF8)) 10 { 11 //调用ReadToEnd(一次读完)方法,并输出到控制台 12 Console.Write(sr.ReadToEnd()); 13 } 14 } 15 Console.ReadKey(); 16 }
★读取网页的内容包括了所有代码,我只要有意义的部分如何做?
上天一声惊雷:正则表达式
浙公网安备 33010602011771号