012天(.net学习之路
来源:互联网 发布:电脑安装监视软件 编辑:程序博客网 时间:2024/05/01 18:01
一、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 //把工资进行转换并加上100024 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 }
★读取网页的内容包括了所有代码,我只要有意义的部分如何做?
上天一声惊雷:正则表达式
<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 012天(.net学习之路
- .NET学习之路
- .net学习之路
- .NET学习之路。
- 013天(.net学习之路
- 011天(.net学习之路
- 014天(.net学习之路
- 017天(.net学习之路
- 学习ASP.NET之路
- .net 学习之路 一
- C#.NET学习之路
- .net 学习之路(转)
- 学习ASP.NET之路
- .net新手之学习之路一
- asp.net 学习之路之DataSet
- Visual Studio .NET学习之路
- Visual Studio .NET 学习之路
- ASP.NET程序员学习之路
- 冒泡排序
- 关于组合数学中我的一个猜想
- C程序代码分析
- linux 下 libcurl 下载
- 黑马程序员-基础-多线程
- 012天(.net学习之路
- 【C语言】02-第一个C程序
- MediaPlayer 播放流程
- 【Android 开发】:UI控件之 Spinner 下拉列表控件的使用
- 选择法对10个整数按从小到大顺序排序
- 能说明你的Javascript技术很烂的五个原因
- web api 根据ip 限制在一定时间内.疯狂访问 api
- 打开任何文件的方法(一)
- HTTP协议中几个状态码的含义:1xx(临时响应)