IO系列测试源码

2022-03-29 09:38:12 浏览数 (1)

代码语言:javascript复制
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IO
{
    class Program
    {
        static void Main(string[] args)
        {
            #region File

            // FileStream fs= File.Create("demo.txt");
            // if(File.Exists("demo.txt"))
            //     Console.WriteLine("demo.txt文件已创建");
            // fs.Close();
            //// File.Move("demo1.txt","demo1.txt");
            // File.Delete("demo.txt");

            #endregion

            #region Directory

            //Directory.CreateDirectory("Test");

            //var dirs = Directory.GetFileSystemEntries("Test");
            //foreach (var dir in dirs)
            //{
            //    Console.WriteLine(dir);
            //}

            #endregion

            #region FileInfo

            //FileInfo fi = new FileInfo(@"./demo.txt");


            //if (fi.Exists)
            //{
            //    Console.WriteLine(fi.Name);
            //    Console.WriteLine(fi.Attributes);
            //    fi.Attributes = FileAttributes.Archive;
            //    Console.WriteLine(fi.CreationTime);
            //    Console.WriteLine(fi.CreationTimeUtc);
            //    Console.WriteLine(fi.Extension);
            //    Console.WriteLine(fi.FullName);
            //    Console.WriteLine(fi.LastAccessTime);
            //    Console.WriteLine(fi.LastAccessTimeUtc);
            //    Console.WriteLine(fi.LastWriteTime);
            //    Console.WriteLine(fi.LastWriteTimeUtc);
            //    DirectoryInfo directoryInfo = fi.Directory;
            //    Console.WriteLine(fi.DirectoryName);
            //    Console.WriteLine(fi.IsReadOnly);
            //    Console.WriteLine(fi.Length);
            //}


            #endregion

            #region DirectryInfo

            //DirectoryInfo di=new DirectoryInfo("./");
            //DirectoryInfo parent = di.Parent;
            //DirectoryInfo root = di.Root;

            //Console.WriteLine(di.FullName);
            //Console.WriteLine(parent.FullName);
            //Console.WriteLine(root.FullName);

            #endregion

            #region Stream

            ////读取数据并展示
            //FileStream fs = new FileStream("./demo.txt", FileMode.Open, FileAccess.Read);

            ////fs.Seek(5, SeekOrigin.Begin);

            //int len, i = 0;
            ////存放每次读取的数据
            //byte[] buffer = new byte[5];
            ////存放所有读到的数据,最后将其转换为字符串
            //byte[] data = new byte[fs.Length];
            ////循环读取文件
            //while ((len = fs.Read(buffer, 0, buffer.Length)) != 0)
            //{
            //    for (int j = 0; j < len; j  )
            //    {
            //        data[i  ] = buffer[j];
            //    }
            //}

            //Console.WriteLine(Encoding.UTF8.GetChars(data));

            ////数据写入
            //FileStream write = new FileStream("./demo_copy.txt", FileMode.Create, FileAccess.Write);

            ////重置指针
            //fs.Seek(0, SeekOrigin.Begin);
            //while ((len = fs.Read(buffer, 0, buffer.Length)) != 0)
            //{
            //    write.Write(buffer, 0, len);
            //}

            //fs.Dispose();
            //write.Dispose();            ////读取数据并展示
            //FileStream fs = new FileStream("./demo.txt", FileMode.Open, FileAccess.Read);

            ////fs.Seek(5, SeekOrigin.Begin);

            //int len, i = 0;
            ////存放每次读取的数据
            //byte[] buffer = new byte[5];
            ////存放所有读到的数据,最后将其转换为字符串
            //byte[] data = new byte[fs.Length];
            ////循环读取文件
            //while ((len = fs.Read(buffer, 0, buffer.Length)) != 0)
            //{
            //    for (int j = 0; j < len; j  )
            //    {
            //        data[i  ] = buffer[j];
            //    }
            //}

            //Console.WriteLine(Encoding.UTF8.GetChars(data));

            ////数据写入
            //FileStream write = new FileStream("./demo_copy.txt", FileMode.Create, FileAccess.Write);

            ////重置指针
            //fs.Seek(0, SeekOrigin.Begin);
            //while ((len = fs.Read(buffer, 0, buffer.Length)) != 0)
            //{
            //    write.Write(buffer, 0, len);
            //}

            //fs.Dispose();
            //write.Dispose();

            #endregion

            #region StreamReader & StreamWriter

            //FileStream fs = new FileStream("./demo.txt", FileMode.Open, FileAccess.ReadWrite);
            //StreamReader sr=new StreamReader(fs,true);

            //StreamWriter sw=new StreamWriter("./demo_copy.txt");

            //string data=String.Empty;
            //while ((data=sr.ReadLine())!=null)
            //{
            //    Console.WriteLine(data);
            //    sw.WriteLine(data);
            //}

            //fs.Dispose();
            //sw.Dispose();
            //sr.Dispose();

            #endregion

            #region 异步操作

            //FileStream fs = new FileStream("./demo.txt", FileMode.Open, FileAccess.Read);
            //Task<string> file = ReadFile(fs);
            //file.ContinueWith(t =>
            //{
            //    Console.WriteLine(t.Result);
            //});

            #endregion

            #region 压缩文件

            //目标文件
            StreamReader sr=new StreamReader("./demo.txt");
            //压缩后文件
            FileStream fw=new FileStream("./demo_zip.txt.zip",FileMode.OpenOrCreate,FileAccess.Write);
            
            //基于FileStream创建压缩对象
            GZipStream gs=new GZipStream(fw,CompressionMode.Compress);
            
            //写入压缩后的数据
            StreamWriter sw=new StreamWriter(gs);
            string data=string.Empty;
            while ((data=sr.ReadLine())!=null)
            {
                sw.Write(data);
            }
            //必须关闭写入对象,否则会导致压缩文件失败
            sw.Close();

            #endregion


            Console.ReadKey();
        }

        private async static Task<string> ReadFile(FileStream fs)
        {
            int len, i = 0;
            byte[] buffer = new byte[5];
            byte[] data = new byte[fs.Length];
            while ((len =await fs.ReadAsync(buffer, 0, buffer.Length)) != 0)
            {
                for (int j = 0; j < len; j  )
                {
                    data[i  ] = buffer[j];
                }
            }

            return Encoding.UTF8.GetString(data);
        }

    }
}

整体的代码,需要自取。

0 人点赞