C#實現文字檔字元過濾

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   for   

本人初學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

 

C#實現文字檔字元過濾

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.