【原创】C#零基础学习笔记010-数据流技术

其他路径:

CSDN: https://blog.csdn.net/wodehao0808

微信公众号:程序喵星人

更多资源和视频教程,QQ:1902686547

10 数据流技术

在应用程序中,进程需要文件来保存数据,这就要用到对文件的输入/输出操作。本章主要介绍和文件相关的类,以及目录、文件操作和创建、读/写文件的方法

10.1 文件操作

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;  // 这是一个处理文件流的命名空间

// 数据流技术:文件的操作

namespace Lesson_38_1

{

    class Program

    {

        static void Main(string[] args)

        {

            /*           

            // example_1: 文件的创建和删除

            // 创建一个文件。File类,File类是静态类。

            string strFilePath = "D:\test.txt";

            if (!File.Exists(strFilePath))

            {

                File.Create(strFilePath);  // 创建文件

            }           

            Console.WriteLine("创建文件成功");

            // File.Delete(strFilePath);  // 删除文件

            // Console.WriteLine("删除文件成功");

            */

            /*

            // example_2: 文件夹的创建和删除

            string strDir = "D:\testCS";

            //Directory.CreateDirectory(strDir);

            //Console.WriteLine("创建文件夹成功");

            Directory.Delete(strDir);

            Console.WriteLine("删除文件夹成功");

            */

            // example_3: 文件的读写操作

            string strFilePath = "D:\test.txt";

            // 需要向文件写入数据

            // 流的概念:指的是字符或字节序列的集合。

            FileStream fstream = new FileStream(strFilePath, FileMode.OpenOrCreate);

            StreamWriter swriter = new StreamWriter(fstream);

            swriter.Write("hello,这是通过文件流写入的数据");

            // 流使用完毕后,需要关闭和释放流资源

            swriter.Close();  // 关闭

            fstream.Close();  // 关闭

            swriter.Dispose();  // 释放

            fstream.Dispose();  // 释放

            Console.WriteLine("写入数据完毕");

            // 从文件读取数据

            StreamReader sreader = new StreamReader(strFilePath);  // 可以直接使用路径创建 读 或 写

            string str = sreader.ReadToEnd();

            Console.WriteLine("取到的数据:" + str);

            sreader.Close();

            sreader.Dispose();

            Console.WriteLine("取数据完毕");

        }

    }

}

10.2 文件流的操作

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;  // 这个命名空间,就是我们对流的操作

// 文件流的操作

namespace Lesson_39_1

{

    class Program

    {

        static void Main(string[] args)

        {

            /*

            // example_1

            // 文件流(字符或字节序列的集合)

            FileStream fstream = new FileStream("D:\test.txt", FileMode.OpenOrCreate);

            StreamReader freader = new StreamReader(fstream);

            string str = freader.ReadToEnd();

            Console.WriteLine("读出的数据:" + str);

            freader.Close();  // 关闭

            fstream.Close();  // 关闭

            freader.Dispose();  // 释放

            fstream.Dispose();  // 释放

            */

            /*

            // example_2

            // 读取文件的第二种方式:转成字节码然后再经过编码处理,通过编码处理之后得到数据。

            FileStream fstream = new FileStream("D:\test.txt", FileMode.OpenOrCreate);

            // 构建字节码

            Byte[] bytes = new Byte[fstream.Length];

            // 把流的数据读取到字节码

            fstream.Read(bytes, 0, (int)fstream.Length);  // 如果length太大,则可以采用一行一行读取

            // 此时需要转码操作,按照一定的编码格式将字节码转换为字符串

            string str = System.Text.Encoding.UTF8.GetString(bytes);

            Console.WriteLine("经过字节转码后的数据:" + str);

            fstream.Close();

            fstream.Dispose();

            */

            // example_3

            // 通过字节转码,将数据写入到文件中

            FileStream fstream = new FileStream("D:\test.txt", FileMode.OpenOrCreate);

            Console.WriteLine("请输入你想输入的数据:");

            string strGet = Console.ReadLine();

            // 把字符串数据转码,转成字节数组

            Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strGet);

            // 把字节数组写入到文件中

            fstream.Write(bytes, 0, bytes.Length);

            fstream.Flush();  // 清楚缓存区的内容

            fstream.Close();

            fstream.Dispose();

        }

    }

}