c#学习记录-文件操作

文件操作/文件夹操作

文件夹会创建在\bin\Debug\net6.0

namespace _23_文件操作
{
    internal class Program
    {
        static void Main(string[] args)
        {
            -------------------文件操作File--------------
            //复制方法1
            FileInfo myFile = new FileInfo("E:\\VScodes\\第四季-高级编程\\23-文件操作\\TextFile1.txt");
            if (!myFile.Exists)
                myFile.CopyTo("E:\\VScodes\\第四季-高级编程\\23-文件操作\\TextFile2.txt");
            //复制方法2
            if (!myFile.Exists)
                File.Copy("E:\\VScodes\\第四季-高级编程\\23-文件操作\\TextFile1.txt",
                    "E:\\VScodes\\第四季-高级编程\\23-文件操作\\TextFile3.txt");
            
            //修改时间
            myFile.CreationTime = DateTime.Now;
            myFile.LastAccessTime = DateTime.Now;
            myFile.LastWriteTime = DateTime.Now;
            Console.WriteLine(myFile.Extension);
            Console.WriteLine(myFile.FullName);
            Console.WriteLine(myFile.Length);


            -----------------文件夹操作Directory--------------
            DirectoryInfo myDirectory = new DirectoryInfo(@"create");
            myDirectory.Create();
            Console.WriteLine(myDirectory.FullName);
            Directory.CreateDirectory("E:\\VScodes\\第四季-高级编程\\23-文件操作\\bin\\Debug\\net6.0\\create\\sondirectory");
            if (myDirectory.FullName != "E:\\VScodes\\第四季-高级编程\\23-文件操作\\bin\\Debug\\net6.0\\create")
                myDirectory.MoveTo("newcreate");
            Console.WriteLine(myDirectory.FullName);
            FileSystemInfo[] fileInfo = myDirectory.GetFileSystemInfos();
            foreach (var item in fileInfo)
            {
                Console.WriteLine(item.FullName);
            }
            DirectoryInfo myFolder = new DirectoryInfo(@"c:\program files");
            
        }
    }
}

流操作

流是比喻成水流,一片海很难一次性运动,但一瓢一瓢舀还是能移动

流是一个用于传输数据的对象,数据可以向两个方向传输:
如果数据从外部源传输到程序中,这就是读取流
如果数据从程序传输到外部源中,这就是写入流
FileStream(文件流) 这个类用于任意文件(包括二进制文件)中读写。
StreamReader(流读取器)和StreamWriter(流写入器)专门用于读写文本文件

文件的操作是消耗内存的,如果不用流,大文件会占很多内存,用流可以减少内存消耗

新建FileStream流,需要3个参数,具体可以查阅资料【C# 教程系列第 19 篇】FileMode,FileAccess 的参数释义_c# filemode_Allen Su的博客-CSDN博客

操作完,记得关闭流:顺序:先开后关,后开先关;先关闭独立需要依赖的文件,后关闭被依赖文件

Console.WriteLine("-----------------读取写入流--------------");
            FileStream readFileStream = new FileStream("D:\\Halcon安装包\\halcon-12.0-windows-64.exe",FileMode.Open,FileAccess.Read);
            //readFileStream.ReadByte();//一个字节一个字节的读,读完后返回-1

            //复制字节,注意模式,需要先建立一个文件,文件路径是不存在的
            FileStream writeFileStream = new FileStream("E:\\VScodes\\第四季-高级编程\\copyfile.exe", FileMode.Create, FileAccess.Write);
            //writeFileStream.WriteByte(0);

            int nextByte = -1;
            while((nextByte = readFileStream.ReadByte()) != -1)
            {
                //把读到的写入新文件
                writeFileStream.WriteByte((byte)nextByte);
            }
            //先开的流后关,后开先关
            writeFileStream.Close();
            readFileStream.Close();

 修改读取数据:

不再用readbyte和writebyte,改为read,write

readFileStream.Read会放回读取的长度,读完之后返回0

FileStream readFileStream = new FileStream("D:\\Halcon安装包\\halcon-12.0-windows-64.exe", FileMode.Open, FileAccess.Read);
            FileStream writeFileStream = new FileStream("E:\\VScodes\\第四季-高级编程\\copyfile.exe", FileMode.Create, FileAccess.Write);

            DateTime befor1 = DateTime.Now;
            byte[] buffer = new byte[1024];
            int count = -1;
            while((count = readFileStream.Read(buffer,0,1024))!=0)
            {
                writeFileStream.Write(buffer, 0, count);
            }
            DateTime after1 = DateTime.Now;
            TimeSpan ts1 = after1 - befor1;
            writeFileStream.Close();
            readFileStream.Close();
            Console.WriteLine(ts1.TotalMilliseconds);

一般读写流操作

注意创建时:,最后一个参数设置为覆盖模式(false默认),还是追加模式(true)

StreamWriter writer = new StreamWriter(destination, true);//追加的方式添加文本

string sourceFile = "E:\\VScodes\\第四季-高级编程\\23-文件操作\\TextFile2.txt";
            string destination = "E:\\VScodes\\第四季-高级编程\\23-文件操作\\TextFile4.txt";
            StreamReader reader = new StreamReader(sourceFile);
            //StreamWriter writer = new StreamWriter(destination,false);//会覆盖
            StreamWriter writer = new StreamWriter(destination, true);//追加的方式添加文本
            //string line = null;
            //while ((line = reader.ReadLine()) != null)
            //    writer.WriteLine(line);

            string nextLine = reader.ReadLine();//读一行
            Console.WriteLine(nextLine);

            int nextchar = reader.Read();//读一个字符ascii码
            Console.WriteLine(nextchar);

            int nChars = 10;
            char[] charArray = new char[nChars];
            int nCharsRead = reader.Read(charArray, 0, nChars);

            foreach (char c in charArray)
            { Console.WriteLine(c); }

            string restOfStream = reader.ReadToEnd();//把剩下的读完
            Console.WriteLine(restOfStream);


            writer.Close();
            reader.Close();

你可能感兴趣的:(c#,学习,开发语言)