.net流與.net流的應用

2012年4月10日  天氣  有雨

流簡單應用:

        static void Main(string[] args)
        {
            
            //文件流操作
            //streamfile();
            //簡單文件加密
            //simpleencypt();
            //簡單文件解密
            //simpledecype();
            Console.Read();
        }
        //簡單文件加密
        static void simpleencypt()
        {
            byte cy = 3;
            byte[] b = File.ReadAllBytes("c:\\abc.txt");
            for (int i = 0; i < b.Length; i++)
            {
                b[i] += cy;
            }
            File.WriteAllBytes("c:\\abc.txt", b);
        }
        //簡單文件解密
        static void simpledecype()
        {
            byte cy = 3;
            byte[] b = File.ReadAllBytes("c:\\abc.txt");
            for (int i = 0; i < b.Length; i++)
            {
                b[i] -= cy;
            }
            File.WriteAllBytes("c:\\abc.txt", b);
        }
        //列出一個文件夾以及子目錄下的所有文件
        static void listAll(string str)
        {
            string[] line = Directory.GetFiles(str);
            foreach (string s in line)
            {
                Console.WriteLine(s);
            }
            string[] lines = Directory.GetDirectories(str);
            foreach(string s in lines)
            {
                listAll(s);
            }
        }
        static void searchEXE()
        {
            //找出一個文件夾以及子目錄下所有exe文件
            string[] lines = Directory.GetFiles("c:\\Windows\\", "*.exe", SearchOption.TopDirectoryOnly);
            foreach (string line in lines)
            {
                Console.WriteLine(line);
            }
        }
        static void Pathclassoperation()
        {
            // string s= File.ReadAllText("c:\\abc.txt",Encoding.Default);//默認是以UTF-8打開
            // Console.WriteLine(s);
            //// Directory.Delete("c:\\abc");
            // if (!Directory.Exists("c:\\zhangfeng\\xt\\zh\\"))
            // {
            //     Directory.CreateDirectory("c:\\zhangfeng\\xt\\zh\\");//級聯創建
            // }
            // listAll("c:\\CodematicDemo\\");
            //searchEXE();
            //更改文件擴展名,只是純字符串改
            string s = Path.ChangeExtension("c:\\abc\\bdfdfd.", "bmp");
            Console.WriteLine(s);
            //連接兩個路徑
            Console.WriteLine(Path.Combine("c:\\a", "cv\\a.bmp"));//c:\a\cv\a.bmp
            //獲取文件的路徑
            Console.WriteLine(Path.GetDirectoryName("c:\\abc\\a.bmp"));//c:abc
            //獲得文件的擴展名
            Console.WriteLine(Path.GetExtension("c:\\abc\\dv.bmp"));//.bmp
            //Path類的靜態方法,主要是一個文件路徑的靜態操作
            //也有一些不同的,這是獲取絕對路徑
            Console.WriteLine(Path.GetFullPath("../../a.exe"));
            //反射中的該程序運行的本地路徑
            Console.WriteLine(Assembly.GetExecutingAssembly().Location);
        }
        static void streamfile()
        {
            //讀到後的路徑
            using (FileStream fs = new FileStream("c:\\ab.avi", FileMode.Create))
            {
                //要讀的文件
                using (FileStream fsread = new FileStream("c:\\abc.avi", FileMode.Open))
                {
                    //設置一次讀的大小
                    byte[] b=new byte[1024];//緩衝區大小
                    //每次讀的長度
                    int blength = 0;
                    //遞歸讀
                    while ((blength=fsread.Read(b, 0, b.Length))>0)
                    {
                        fs.Write(b, 0, blength);//寫到文件中
                    }
                }
            }
            //另外的一種耗內存的方法
            //不管文件大小,一次性讀到內存,再從內存中復出來
            byte[] e = File.ReadAllBytes("c:\\abc.avi");
            File.WriteAllBytes("c:\\a.avi",e);
        }


流高級:

        static void simple1()
        {
            FileStream fs = new FileStream("c:\\1.txt", FileMode.Create);
            string s = "hellotext";
            byte[] b = Encoding.UTF8.GetBytes(s);
            fs.Write(b, 0,b.Length);//因爲寫字節流的時候,有一個緩衝區的東西,他並沒有寫入文件,而是寫入緩衝區中
            //fs.Flush();//清除緩衝區,把字節流放入文件
            //fs.Close();//此方法,關閉字節流,先調用flush()
            fs.Dispose();//釋放資源,先調用close(),
        }
        //壓縮與解壓
        #region 壓縮與解壓
        //壓縮  壓縮後 打開文本文件是 一個亂碼
        static void simple2_1()
        {
            string s = "好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好好";
            for (int i = 0; i < 15; i++)
            {
                s += s;
            }//設置一個字符串,因爲要足夠大,才能看得出壓縮的效果,所有使用for語句
            using (FileStream fs = File.OpenWrite("C:\\1.txt"))//定義一個打文件流
            {
                using (GZipStream gz = new GZipStream(fs, CompressionMode.Compress))//定義一個壓縮流
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(s);//把文字轉換成字節
                    gz.Write(bytes, 0, bytes.Length);//寫入文件
                }
            }
        }
        //解壓  
        static void simple2_2()
        {
            using (FileStream fs = new FileStream("c:\\1.txt", FileMode.Open))//定義打開文件流
            {
                using (GZipStream zp = new GZipStream(fs, CompressionMode.Decompress))//對文件流進行解密
                {
                    byte[] b = new byte[1024];
                    int readbyte=0;
                    using (FileStream outstream = new FileStream("c:\\2.txt", FileMode.Create))//新建一個文件流用於輸出
                    {
                        while((readbyte=zp.Read(b,0,1024))>0)
                        {
                            outstream.Write(b,0,readbyte);
                        }
                    }
                }
            }
        }
        #endregion

        //內存流   把字節放入內存中
        static void simple3()
        {
            MemoryStream mo = new MemoryStream();
            string s = "agasjfdf";
            byte[] b = Encoding.UTF8.GetBytes(s);
            mo.Write(b, 0, b.Length);
        }

        //文本讀取
        static void simple4()
        {
            using (FileStream fs = new FileStream("c:\\abc.txt", FileMode.Open))
            {
                //因爲它在string和byte[]之間轉換,所以要指定編碼
                using (StreamReader sr = new StreamReader(fs,Encoding.UTF8))//streamreader專用於文本讀取,讀取前先打開一個文件流
                {
                    //string s = "";
                    //while ((s=sr.ReadLine())!=null)//readline讀取一行,結束返回null
                    //{
                    //    Console.WriteLine(s);
                    //}
                    string s = sr.ReadToEnd();
                    Console.WriteLine(s);
                    
                }
            }
        }
        //流高級中網絡流與內存流中切換讀取,讀服務器上的excel文件
        //作此實驗  要建一個excel文件  讀取第一行第一列值
        //還要用cassiniDev server  建一個小型web服務器
        static void simple5()
        {
            WebClient wc = new WebClient();
            //wc.DownloadFile("http://localhost:32768/abc.xls", @"c:\\abc.xls");//因爲擔心安全問題,所以採用流讀取,可以下載下來直接打開
            Stream stream= wc.OpenRead("http://localhost:32768/abc.xls");//因返回的是connectstream流,但他不支持來回讀取,此流不能用NPOI讀,所以把網絡流讀到內存中
            MemoryStream ms;//定義內存流
            using (stream)
            {
                ms = new MemoryStream();//建一個網絡流
                byte[] bytes = new byte[1024];
                int readbytes;
                while((readbytes=stream.Read(bytes,0,1024))>0)//把網絡流中複製到內存流中
                {
                    ms.Write(bytes, 0, readbytes);//一次讀寫1K
                }
            }
            using (ms)
            {
                HSSFWorkbook workbook = new HSSFWorkbook(ms);//用工作薄接收內存流
                HSSFSheet sheet = workbook.GetSheetAt(0);//取第一個表
                HSSFRow row = sheet.GetRow(0);//取第一行
                HSSFCell hc = row.GetCell(0);//取第一列
                Console.WriteLine(hc.StringCellValue);//輸出 王小明
            }
        }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章