zoukankan      html  css  js  c++  java
  • 08---Net基础加强

    资料管理器作业

     public partial   class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                ////获取当前正在执行的程序集(exe文件)的路径
                //string exePath = Assembly.GetExecutingAssembly().Location;
                ////加载文件结构到TreeView
                ////得到demo文件夹的完整路径
                //string path = Path.Combine(Path.GetDirectoryName(exePath), "demo");
    
                string path = @"F:360云盘";
                LoadDataToTree(path, treeView1.Nodes);
           
            }
    
            private void LoadDataToTree(string path, TreeNodeCollection treeNodeCollection)
            {
                //1.获取path下的所有子文件和子文件夹的路径
                string[] files = Directory.GetFiles(path, "*.txt");
                string[] dirs = Directory.GetDirectories(path);
    
                //2.遍历文件与文件夹加载到TreeView上
                //遍历文件
                foreach (string item in files)
                {
                    TreeNode node = treeNodeCollection.Add(Path.GetFileName(item));
                    //如果是文件节点,则把当前节点的完整路径保存在节点的Tag属性中
                    node.Tag = item;
                }
    
                //遍历文件夹
                foreach (string item in dirs)
                {
                    TreeNode node = treeNodeCollection.Add(Path.GetFileName(item));
                    //递归调用
                    LoadDataToTree(item, node.Nodes);
                }
            }
    
            //TreeView的节点的鼠标双击事件
            private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
            {
                if (e.Node.Tag != null)//表示点击的是文件节点
                {
                    //获取当前双击的节点的Tag中存储的路径
                    string currentPath = e.Node.Tag.ToString();
                    //e.Node.Parent
                    //根据该路径读取对应的文件内容
                    string txt = File.ReadAllText(currentPath, Encoding.Default);
                    textBox1.Text = txt;
                }
            }
        }

    效果图

    类型推断

     class Program
        {
            //static var Age;
            static void Main(string[] args)
            {
                #region MyRegion
                //强类型,在编译的时候就已经能确定数据类型了。
                //string n = "aaaa";
                //n = 10;
    
                //弱类型,程序编译的时候无法确定数据类型,在运行的时候才会确定数据类型。
                //n = "aaaa";
                //n=10;
    
                //dynamic d = new ExpandoObject();
                //d.Name = "张三";
                //d.Age = 19;
    
                //Console.WriteLine(d.Name);
                //Console.WriteLine(d.Age);
                //Console.Read();
                #endregion
    
                #region C#中的 var 是强类型
    
                //var依然是强类型的,
                //编译器会在编译的时候将其替换为对应的数据类型
                //编译器根据“等号”右边的数据的类型自动推断出应该是什么数据类型,就会将var替换成对应的数据类型。
                //var这种类型推断只能应用在方法的局部变量中
                //不能将var应用于类的成员变量、方法的参数、方法的返回值
                //var n = 100;
                //var s = "aaaa";
                ////int n = 100;
    
                #endregion
    
                string[] names = new string[] { "a", "b" };
                foreach (var item in names)
                {
    
                }
    
                List<int> list = new List<int>();
                foreach (var item in list)
                {
    
                }
    
                //ArrayList arrlist = new ArrayList();
                //foreach (var item in arrlist)
                //{
    
                //}
    
                //Hashtable hash = new Hashtable();
                //foreach (var item in hash.Keys)
                //{
    
                //}
    
                //foreach (var item in hash.Values)
                //{
    
                //}
    
    
                ////这里的var应该是DictionaryEntry类型,但类型推断却是object
                //foreach (var item in hash)
                //{
    
                //}
    
                Dictionary<string, int> dict = new Dictionary<string, int>();
                foreach (var item in dict.Keys)
                {
    
                }
    
                foreach (var item in dict.Values)
                {
    
                }
    
                //这里的var可以正常推断出来var表示的是KeyValuePair<string,int> 类型。
                foreach (var item in dict)
                {
    
                }
            }
            //static void M1(var n)
            //{
    
            //}
    
            //static var M2()
            //{
            //    return "aaa";
            //}
        }

    操作文件File    程序集 using System.IO;

      class Program
        {
            static void Main(string[] args)
            {
                ////1.判断一个文件是否存在
                //bool b = File.Exists(@"c:hello.txt");
                //Console.WriteLine(b);
                //Console.Read();
    
                ////2.拷贝一个文件
                //File.Copy(@"c:hello.txt", @"d:hello.txt");
                //Console.WriteLine("ok");
                //Console.Read();
                //File.Move(); 移动,剪切文件
    
                ////3.创建一个文件
                //File.Create(@"c:abc.txt");
                //Console.WriteLine("ok");
                //Console.Read();
    
                ////4.删除一个文件
                ////文件的删除,即便文件不存在也不会报异常
                //File.Delete(@"c:abc.txt");
                //Console.WriteLine("ok");
                //Console.Read();
    
                //5.读取一个文件,写入
                //File.WriteAllText(@"c:xxx.txt", "你好China !");
                //string[] lines=new string[]{"aaaaaaaaaaa","bbbbbbbbbbb","cccccccccc"};
                //File.WriteAllLines(@"c:yyy.txt",lines);
    
                ////把一个字符串转换为一个byte[]
                //string msg = "哈喽沃尔德你好世界Hello World、!!!!!";
                //byte[] bytes = System.Text.Encoding.UTF8.GetBytes(msg);
    
                //File.WriteAllBytes(@"c:ooo.txt", bytes);
                //Console.WriteLine("ok");
                //Console.Read();
    
                //看两个方法有什么区别,首先看这两个方法的功能有什么不同,然后看这两个方法的参数与返回值有什么不同,最后使用reflector 看看微软的内部实现有什么不同。
                //读取字符串
                //File.ReadAllText();
                //File.ReadAllLines();
    
                //byte[] byts = File.ReadAllBytes(@"c:ooo.txt");
                ////把byte[]数组转换为字符串
                //string msg = System.Text.Encoding.UTF8.GetString(byts);
                //Console.WriteLine(msg);
                //Console.Read();
    
                //////File.ReadAllText(,Encoding.Default
                ////Encoding e = Encoding.Default;
                //string msg = "";
                //File.WriteAllText("", "", Encoding.GetEncoding("gb2312"));
            }
        }

    文件编码

      public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                ////byte[] bytes = File.ReadAllBytes("1.txt");
                //string msg = File.ReadAllText("1.txt");
    
                ////Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
                //MessageBox.Show(msg);
    
                StringBuilder sb = new StringBuilder();
                EncodingInfo[] infos = Encoding.GetEncodings();
                foreach (var item in infos)
                {
                    sb.AppendLine(item.CodePage + "	" + item.Name + "	" + item.DisplayName);
                }
                File.WriteAllText("encodings.txt", sb.ToString());
                MessageBox.Show("ok");
            }
        }

    文件流

        class Program
        {
            static void Main(string[] args)
            {
                #region FileStream文件流的使用方式
    
                ////1.创建一个    中国.txt
                //string txt = "中国是世界上人口第一大国。中国是世界上最幸福的国家之一。中国是四大文明古国之一。中国有个叶长种。";
    
                ////一、创建一个文件流
                //FileStream fs = new FileStream(@"F:360云盘中国.txt", FileMode.Create, FileAccess.Write);
                //byte[] buffer = Encoding.UTF8.GetBytes(txt);
    
                ////二、读文件或者写文件
    
                ////参数1:表示要把哪个byte[]数组中的内容写入到文件
                ////参数2:表示要从该byte[]数组的第几个下标开始写入,一般都是0
                ////参数3:要写入的字节的个数。
                //fs.Write(buffer, 0, buffer.Length);
    
                //////三、关闭文件流
                //////清空缓冲区
                ////fs.Flush();
    
                ////fs.Close();
    
                ////四、释放相关资源
                //fs.Dispose();
    
                //Console.WriteLine("ok");
                //Console.ReadKey();
    
                //=================================================================
    
                ////1.创建一个    中国.txt
                //string txt = "中国是世界上人口第一大国。中国是世界上最幸福的国家之一。中国是四大文明古国之一。中国有个叶长种。";
    
                ////一、创建一个文件流
                ////当把一个对象放到using()中的时候,当超出using的作用于范围后,会自动调用该对象的Dispose()f方法。
                //using (FileStream fs = new FileStream(@"c:中国.txt", FileMode.Create, FileAccess.Write))
                //{
    
                //    byte[] buffer = Encoding.UTF8.GetBytes(txt);
                //    //二、读文件或者写文件
                //    //参数1:表示要把哪个byte[]数组中的内容写入到文件
                //    //参数2:表示要从该byte[]数组的第几个下标开始写入,一般都是0
                //    //参数3:要写入的字节的个数。
                //    fs.Write(buffer, 0, buffer.Length);
                //}
                //Console.WriteLine("ok");
                //Console.ReadKey();
    
                #endregion
    
                #region using使用
    
                ////using (Person p=new Person())
                ////{
                //            //using中的代码
                ////}
    
                //Person p = new Person();
                //try
                //{
                //    //using中的代码
                //}
                //finally
                //{
                //    p.Dispose();
                //}
    
                //Console.WriteLine("ok");
                //Console.ReadKey();
                #endregion
    
                #region using + FileStream
    
                ////创建文件流
                //using (FileStream fs = new FileStream("世界.txt", FileMode.Create))
                //{
                //    string world = "你好世界!";
                //    byte[] buffer = Encoding.UTF8.GetBytes(world);
                //    //2.读写文件
                //    fs.Write(buffer, 0, buffer.Length);
    
                //}
                //Console.WriteLine("ok");
                //Console.ReadKey();
    
                #endregion
    
                #region 使用文件流进行拷贝
    
                string source = @"e:全面回忆.rmvb";
                string target = @"F:全面回忆.rmvb";
    
                CopyFile(source, target);
                Console.WriteLine("ok");
                Console.ReadKey();
    
                #endregion
    
            }
    
            private static void CopyFile(string source, string target)
            {
                //1.创建一个读取源文件的文件流
                using (FileStream fsRead = new FileStream(source, FileMode.Open, FileAccess.Read))
                {
                    //2.创建一个写入新文件的文件流
                    using (FileStream fsWrite = new FileStream(target, FileMode.Create, FileAccess.Write))
                    {
    
                        //创建缓冲区
                        byte[] buffer = new byte[1024 * 1024 * 5];//更方便阅读
                        //3.通过fsRead读取源文件,然后再通过fsWrite写入新文件
    
                        //通过文件流读取
                        //参数1:表示将来读取到的内容要存放到哪个数组中
                        //参数2:表示这个数据要从第几个索引开始填充数据、
                        //参数3:表示本次读取最多可以读取多少个字节。
                        //返回值:表示本次实际读取到的字节个数。
                        int byteCount = fsRead.Read(buffer, 0, buffer.Length);
                        while (byteCount > 0)
                        {
                            //把刚刚读取到的内容写入到新文件流中
                            fsWrite.Write(buffer, 0, byteCount);
    
                            //需要循环执行读写操作
    
                            //把上次读取到内容写入完毕后,继续再读取
                            byteCount = fsRead.Read(buffer, 0, buffer.Length);
                            //fsRead.Position
                            //fsWrite.Position
                            Console.Write(". ");
                        }
                    }
                }
            }
        }
        class Person : IDisposable
        {
    
            #region IDisposable 成员
    
            public void Dispose()
            {
                //throw new NotImplementedException();
                Console.WriteLine("Dispose()方法被调用了。。。。。");
            }
    
            #endregion
        }

    文件拷贝复习

        class Program
        {
            static void Main(string[] args)
            {
                string source = @"F:Documents and SettingsAdministrator桌面心花怒放.avi";
                string target = @"F:心花怒放.avi";
                CopyFile(source, target);
                Console.WriteLine("ok");
                Console.ReadKey();
            }
    
            private static void CopyFile(string source, string target)
            {
                //创建两个文件流,一个读取,一个写入
    
                //读取文件的流
                using (FileStream fsRead = new FileStream(source, FileMode.Open, FileAccess.Read))
                {
                    //byte b = 19;
                    //byte bb = (byte)(byte.MaxValue - b);
    
                    //获取原文件的大小
                    long len = fsRead.Length;
    
                    //写入文件流
                    using (FileStream fsWrite = new FileStream(target, FileMode.Create, FileAccess.Write))
                    {
                        //创建一个缓冲区
                        byte[] buffer = new byte[1024 * 1024 * 2];
    
                        //通过fsRead流读取一部分数据到buffer中
                        int r = fsRead.Read(buffer, 0, buffer.Length);
                        while (r > 0)
                        {
                            Console.Write(". ");
                            //将读取到的内容写入到fsWrite流中
                            fsWrite.Write(buffer, 0, r);
                            double d = fsWrite.Position * 1.0 / len;
                            Console.WriteLine("已经拷贝完毕了:{0}%", d * 100);
    
                            r = fsRead.Read(buffer, 0, buffer.Length);
                        }
                    }
                }
            }
        }

    文件流加密解密   ----下面的加密两次就解密了

      public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string source = textBox1.Text.Trim();
                string target = textBox2.Text.Trim();
    
                CopyEncryptFile(source,target);
                MessageBox.Show("OK");
            }
    
            private void CopyEncryptFile(string source, string target)
            {
                //创建两个文件流,一个读取,一个写入
    
                //读取文件的流
                using (FileStream fsRead = new FileStream(source, FileMode.Open, FileAccess.Read))
                {
                
                    //写入文件流
                    using (FileStream fsWrite = new FileStream(target, FileMode.Create, FileAccess.Write))
                    {
                        //创建一个缓冲区
                        byte[] buffer = new byte[1024 * 1024 * 2];
    
                        //通过fsRead流读取一部分数据到buffer中
                        int r = fsRead.Read(buffer, 0, buffer.Length);
    
                        //=========================加密=======================
                        for (int i = 0; i < r; i++)
                        {
                            buffer[i] = (byte)(byte.MaxValue - buffer[i]);
                        }
                        while (r > 0)
                        {
                            //将读取到的内容写入到fsWrite流中
                            fsWrite.Write(buffer, 0, r);
                            r = fsRead.Read(buffer, 0, buffer.Length);
                        }
                    }
                }
            }
        }

    StreamReader与StreamWriter

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    namespace _专门对文本文件操作的文件流
    {
        class Program
        {
            static void Main(string[] args)
            {
                //如果对文本文件需要读取一部分显示一部分则使用FileStream会有问题,
                //因为可能FIleStream会在读取的时候吧一个汉字的字节数给分开,所以造成显示的时候无法正确显示字符串
                //所以读取大文本文件一般使用StreamReader类
                //对于大文本文件写入一般用StreamWriter类。
    
                #region StreamWriter类的使用
    
                ////1.创建一个StreamWriter
                //using (StreamWriter sw=new StreamWriter("text.txt",false,Encoding.UTF8))
                //{
                //    //2.执行读写
                //    for (int i = 0; i < 1000; i++)
                //    {
                //        sw.WriteLine(i+ "=========" + System.DateTime.Now.ToString() );
                //    }
                
                //}
                //Console.WriteLine("OK");
    
                #endregion
    
    
                #region StreamReader类的使用
    
                using (StreamReader reader = new StreamReader("英汉词典TXT格式.txt",Encoding.Default))
                {
                  //while(!reader.EndOfStream)
                  //{
                  //    string line = reader.ReadLine();
                  //    Console.WriteLine(line);
                  //}
    
                    string line = null;
                    int count = 0;
                    while((line=reader.ReadLine())!=null)
                    {
                        count++;
                        Console.WriteLine(line);
                    }
                    Console.WriteLine(count);
    
                }
    
                Console.WriteLine("OK");
                Console.ReadKey();
    
                #endregion
            }
        }
    }

    StreamReader与StreamWriter练习

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    namespace _工资翻倍练习
    {
        class Program
        {
            static void Main(string[] args)
            {
                //1.读取
                using (StreamReader reader = new StreamReader("工资文件.txt", Encoding.Default))
                {
    
    
                    //2.写入
                    using (StreamWriter write = new StreamWriter("新工资文件.txt", false, Encoding.UTF8))
                    {
                        string line = null;
                        while ((line = reader.ReadLine()) != null)
                        {
                            string[] parts = line.Split('|');
                            string newLine = parts[0] + "|" + Convert.ToInt32(parts[1]) * 2;
                            write.WriteLine(newLine);
                        }
                    }
                  
                }
    
                Console.WriteLine("OK");
                Console.ReadKey();
            }
        }
    }

    压缩-----有问题以后再解决!!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.IO.Compression;
    
    namespace _压缩流介绍
    {
        class Program
        {
            static void Main(string[] args)
            {
                //StringBuilder sb = new StringBuilder("中华人民共和国");
                //for (int i = 0; i < 10; i++)
                //{
                //    sb.Append(sb.ToString()); 
                //}
                //File.WriteAllText("1.txt",sb.ToString());
                //Console.WriteLine("OK");
    
                #region 压缩
    
                using (FileStream fsReader = File.OpenRead("1.txt"))
                {
                    using (GZipStream zip = new GZipStream(fsReader, CompressionMode.Compress))
                 {
                     using (FileStream fsWrite = File.OpenWrite("yasuo.rar"))
                     {
                         byte[] buffer = new byte[1024 * 1024 * 2];
                         int r = zip.Read(buffer, 0, buffer.Length);
                         while (r > 0)
                         {
                             fsWrite.Write(buffer, 0, r);
                         }              
                     }                 
                 }
                }
                Console.WriteLine("OK");
                #endregion
            }
        }
    }

    序列化

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _序列化
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Person> list = new List<Person>()
                {
                    new Person(){Name="张三",Age=18},
                    new Person(){Name="李四",Age=19},
                    new Person(){Name="王五",Age=20},
                };
    
                //序列化就是指格式化的过程
                //对象序列化:对象格式化,把某个对象,使用另外一个格式来存储,更方便数据交换。
                //二进制序列化
                //xml序列化
                //JavaScript序列化
    
                //反序列化
    
            }
        }
    
        class Person
        {
            public string Name
            {
                get;
                set;
            }
    
            public int Age
            {
                get;
                set;
            }
        }
    }
  • 相关阅读:
    作业17
    模块
    Find the Lost Sock (异或算法)
    CD(二分)
    数字流输入
    最大连续子序列(dp)
    STL学习----lower_bound和upper_bound算法
    输入挂(减少时间)
    暴力之全排列
    【C++】判断元素是否在vector中,对vector去重,两个vector求交集、并集
  • 原文地址:https://www.cnblogs.com/yechangzhong-826217795/p/4105085.html
Copyright © 2011-2022 走看看