码迷,mamicode.com
首页 > Windows程序 > 详细

C#实现文本文件字符过滤

时间:2014-11-21 12:05:45      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

本人初学C#,最近才接触数组、流,编写了一个小程序。几乎没实用价值,只用来记录自己所学。

bubuko.com,布布扣
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 namespace namespace1   //命名空间
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             FileStream file = File.Open("F:/filename.txt"/*初始文件路径*/, FileMode.Open, FileAccess.Read);//打开初始文件
13             FileStream file2 = File.Open("F:/filename2.txt"/*过滤后文件*/, FileMode.Create, FileAccess.Write);//创建处理后的文件
14             file2.Close();//关闭file2文件流
15             byte[] array = new byte[file.Length];//初始化字节数组
16             file.Read(array, 0, array.Length);//读取流中的数据并把它写入字节数组中
17             file.Close();   //关闭file1文件流  
18             string str = Encoding.Default.GetString(array);  //将字节数组转化为字符串
19             char[] ch = str.ToCharArray();  //将字符串转化为字符数组
20             string str1 = "";           
21             foreach (char c in ch)  //遍历ch 
22             {
23                 if ((c != 1) & (c != 2) & (c != 3) & (c != 4) & (c != 5) & (c != 6) & (c != 7) & (c != 8) & (c != 9) & (c != 0))
24                 //判断字符不等于数字,可根据实际情况更改过滤条件   
25                 {
26                     str1 = str1 + c.ToString();//字符串拼接
27                 }
28             }
29             string fullPath = "F:/filename2.txt";                                  //将字符串写入文件
30             if (System.IO.File.Exists(fullPath)) 
31             {
32                 using (StreamWriter sw = new StreamWriter(fullPath, false, Encoding.Default)) 
33                 {
34                    sw.WriteLine(str1); 
35                 } 
36             }
37         }
38     }
39 }
View Code

 

C#实现文本文件字符过滤

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/pwenlee/p/4112310.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!