zoukankan      html  css  js  c++  java
  • C#中文件操作

        1、获取目录信息

           DirectoryInfo directory = new DirectoryInfo(LogPath);     //不存在不报错  注意exists属性

           Console.WriteLine(string.Format("{0} {1} {2}", directory.FullName, directory.CreationTime, directory.LastWriteTime));

        2、获取文件信息

           FileInfo fileInfo = new FileInfo(Path.Combine(LogPath, "info.txt"));

           Console.WriteLine(string.Format("{0} {1} {2}", fileInfo.FullName, fileInfo.CreationTime, fileInfo.LastWriteTime));

        3、写文件

           public static void Write()

           {

               string fileName = "info.txt";        //指定文件名称

               string totalPath = Path.Combine(LogPath, fileName);    //组建文件的全路径(包括路径和文件名)

               string msg = "今天是Course6IOGDI,今天上课的人有9个1";    //写入的内容

               StreamWriter sw = null;     //文件流

               try

               {

                   if (Directory.Exists(LogPath))     //判断目录文件是否存在

                   {

                       if (File.Exists(totalPath))    //判断文件是否存在

                       {

                           sw = File.AppendText(totalPath);     //存在就追加文本内容,没有就新建

                       }

                       else

                       {

                           sw = File.CreateText(totalPath);     //不存在就新建文件

                       }

                   }

                   else

                   {

                       Directory.CreateDirectory(LogPath);     //如果目录不存在就创建目录

                       sw = File.CreateText(totalPath);        //然后新建文件

                   }

                   sw.WriteLine(msg); //把内容写入文件流

               }

               finally

               {

                   if (sw != null)     //只要文件流不是空的

                   {

                       sw.Flush();     //清理当前编写器的所有缓冲区,并使所有缓冲数据写入基础流。

                       sw.Close();     //关闭文件流

                       sw.Dispose();   //释放内存

                   }

               }

           }

        4、读取文件

           A、一次性读取文件

              public static void Read()

              {

                  string fileName = @"info.txt";       //指定文件名称

                  string totalPath = Path.Combine(LogPath, fileName);    //组建文件的全路径(包括路径和文件名)

                  if (File.Exists(totalPath))         //判断文件是否存在

                  {

                      string[] results = File.ReadAllLines(totalPath);      //打开指定文本文件,将文件的所有行都读入一个字符串数组,然后关闭该文件。

                      foreach (string result in results)                 //遍历读取结果

                      {

                          Console.WriteLine(result);

                      }

                  }

              }

           B、单行读取模式

              using (StreamReader sr = new StreamReader(totalPath))   //用using来关闭对象释放内存,能使用using的对象必须继承了IDisposeable

              {   

                  string oneLine = sr.ReadLine();    //从当前流中读取一行字符并将数据作为字符串返回。

                  while (oneLine != null)            //读到的行资料不是空时继续

                  {

                      Console.WriteLine(oneLine);    //显示

                      oneLine = sr.ReadLine();       //读下一行

                  }

              }

           C、读取大量数据

              public void ReadHug()

              {

                  Byte[] byteData = new byte[100];    //字节数组

                  char[] charData = new char[1000];   //字符数组

                  string fileName = @"info.txt";

                  string totalPath = Path.Combine(LogPath, fileName);

                  FileStream file = new FileStream(totalPath, FileMode.Open);

                  file.Seek(0, SeekOrigin.Begin);     //从开头开始偏移量为0的位置开始读取

                  file.Read(byteData, 0, 100);

                          //byteData是传进来的字节数组,用以接受FileStream对象中的数据,

                          //第2个参数是字节数组中开始写入数据的位置,它通常是0,是byteData中开始读取的偏移量,

                          //最后一个参数规定从文件读多少字符.

                  Decoder d = Encoding.Default.GetDecoder();

                          //声明一个解码器,该解码器将已编码的字节序列转换为字符序列。

                  d.GetChars(byteData, 0, byteData.Length, charData, 0);    //将一个字节序列解码为一组字符。

                          //第1个参数:包含要解码的字节序列的字节数组。

                          //第2个参数:第一个要解码的字节的索引。

                          //第3个参数:要解码的字节数。

                          //第4个参数:要用于包含所产生的字符集的字符数组。

                          //第5个参数:开始写入所产生的字符集的索引位置。

                  Console.WriteLine(charData);

                  file.Close();

              }

           D、文件复制

              File.Copy(totalPath, totalPath.Replace("info.txt", "info_copy.txt"));

           E、删除文件

              File.Delete(totalPath);

           D、使用递归方法查找所有文件

              public static void RecursionShow()

              {

                  string rootPath = @"E:online_5";           //指定要查询的目录

                  DirectoryInfo dirRoot = new DirectoryInfo(rootPath);  //获取根目录的目录信息

                  List<FileInfo> fileInfoList = new List<FileInfo>();   //创建文件信息存储List

                  fileInfoList = GetFileByDir(dirRoot, fileInfoList);   //运行获取文件清单的递归程序

              }

              private static List<FileInfo> GetFileByDir(DirectoryInfo dirCurrent, List<FileInfo> fileInfoList)

              {

                  FileInfo[] fileArray = dirCurrent.GetFiles();      //获取当前目录文件信息的数组

                  if (fileArray != null && fileArray.Length > 0)

                  {

                      fileInfoList.AddRange(fileArray.ToList());     //有文件就将他添加到指定集合的末尾。

                  }

                  DirectoryInfo[] dirArray = dirCurrent.GetDirectories();   //获取当前目录中的子目录信息,并保存到数组

                  if (dirArray != null && dirArray.Length > 0)  //判断有无子目录

                  {

                      foreach (DirectoryInfo dir in dirArray)   //有子目录的话遍历这些子目录,并再此获取子目录中的文件

                      {

                          GetFileByDir(dir, fileInfoList);      //自己调用自己,形成递归。

                      }

                 }

                  return fileInfoList;      //返回获得的文件信息List

             }

  • 相关阅读:
    Codeforces
    Codeforces
    SCUT
    模板
    SCUT
    SCUT
    模板
    SCUT
    UVA 437 "The Tower of Babylon" (DAG上的动态规划)
    UVA 1025 "A Spy in the Metro " (DAG上的动态规划?? or 背包问题??)
  • 原文地址:https://www.cnblogs.com/huang3/p/5076846.html
Copyright © 2011-2022 走看看