zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    多屏显示
    Scss sass
    静态页面常用到
    display:flex
    怎么看服务器是属于阿里云的还是腾讯云
    介绍一款好用 mongodb 可视化工具
    图解Mongo Shell的使用
    Win10 安装配置 MongoDB 4.0 踩坑记
    phpexcel来做表格导出(多个工作sheet)及设置单元格格式
    phpexcel 导出方法
  • 原文地址:https://www.cnblogs.com/pwenlee/p/4112310.html
Copyright © 2011-2022 走看看