.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);//输出 王小明
            }
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值