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


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

     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
  • 相关阅读:
    HighTec-Eclipse for Tricore 的安装方法
    http升级https遇到的问题
    symfony中模板生成路径两种方式
    http请求在https中使用
    git tag标签
    Git查看两个版本之间修改了哪些文件
    mysql将语句写入表中
    使用fiddler抓包模拟器及配置fiddler过滤
    Mixed Content: The page at 'xxx' was loaded over HTTPS, but requested an insecure resource 'xxx'.
    nginx 禁止某IP访问
  • 原文地址:https://www.cnblogs.com/pwenlee/p/4112310.html
Copyright © 2020-2023  润新知