飞秋的文件读取、写入代码

来源:互联网 发布:"自己"网络谐音 编辑:程序博客网 时间:2024/05/01 13:21

大家都知道局域网聊天发即时通讯软件是最平常不过的事情,所以大量的导入即时通讯软件是有利于局域网聊天的,飞秋www.freeeim.com 下载在决定一个网站的排名时,不仅要对网页内容和结构进行分析,还围绕网站的即时通讯软件展开分析。对网站排名致关重要的影响因素是获得尽可能多的高质量外部即时通讯软件,也称导入即时通讯软件。

什么是导入即时通讯软件?

 

飞秋的文件读取、写入代码


文件的读操作

   static void Main(string[] args)
        {
            string path = "";
            Console.WriteLine("请输入要读取的文件的文件名,包括路径");
            path = Console.ReadLine();
            if (!File.Exists(path))
            {
                Console.WriteLine("文件不存在");
                return;
            }

            try
            {
                FileStream file = new FileStream(path, FileMode.Open);
                byte[] bt = new byte[file.Length];
                file.Read(bt, 0, bt.Length);
                string str = Encoding.Default.GetString(bt);
                Console.WriteLine(str);
                Console.ReadLine();
            }
            catch (System.Exception e)
            {
                Console.WriteLine("读取文件出错");
            }

        }

 

文件的写操作:

       static void Main(string[] args)
        {
            //FileStream fs1 = File.Create("test1");
            //fs1.Close();
            //Console.ReadLine();

            string path = "";
            string content = "";
            Console.WriteLine("请输入要保存的文件的文件名,包括路径");
            path = Console.ReadLine();
            Console.WriteLine("请输入要保存的内容 ");
            content = Console.ReadLine();
            try
            {
                FileStream file = new FileStream(path, FileMode.Create);
                byte[] bt = Encoding.UTF8.GetBytes(content);
                file.Write(bt, 0, bt.Length);
                file.Flush();
            }
            catch (System.Exception e)
            {
                Console.WriteLine("创建或写入文件时出错");
            
            }

 

 // 读取文件流

     static void Main(string[] args)
        {
            Console.WriteLine("请输入要读取文件的文件名,包括路径");
            string path = Console.ReadLine();
            if (!File.Exists(path))
            {
                Console.WriteLine("文件不存在");
                return;
            }

            FileStream readStream = new FileStream(path, FileMode.Open);
            BufferedStream readBuffered = new BufferedStream(readStream);
            byte[] bt = new byte[readBuffered.Length];
            readBuffered.Read(bt, 0, (int)readBuffered.Length);
            Console.WriteLine(Encoding.Default.GetString(bt));
            readBuffered.Close();
            Console.ReadLine();
        }

原创粉丝点击