IO

实现日志写入、读取,文本复制、删除,递归获取文件夹下所有文件

program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestIO
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                MyIO.ReadInfo();

                MyIO.Write();
                MyIO.Read();
                MyIO.ReadOneLine();
                MyIO.Copy();
                MyIO.Delete();
                MyIO.RecursionShow();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

MyIO.cs

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace TestIO
{
    public class MyIO
    {
        private static string LogPath = ConfigurationManager.AppSettings["LogPath"];

        /// <summary>
        /// 读取文件夹  文件信息
        /// </summary>
        public static void ReadInfo()
        {
            DirectoryInfo directory = new DirectoryInfo(LogPath);//不存在不报错  注意exists属性,为true时存在,为false时不存在
            Console.WriteLine(string.Format("{0} {1} {2}", directory.FullName, directory.CreationTime, directory.LastWriteTime));

            FileInfo fileInfo = new FileInfo(Path.Combine(LogPath, "info.txt"));
            Console.WriteLine(string.Format("{0} {1} {2}", fileInfo.FullName, fileInfo.CreationTime, fileInfo.LastWriteTime));
        }

        public static void Write()
        {
            string fileName = "info.txt";
            string totalPath = Path.Combine(LogPath, fileName);
            string msg = "这里是写入日志的内容";

            StreamWriter sw = null;
            try
            {
                if (Directory.Exists(LogPath))
                {
                    sw = File.AppendText(totalPath);
                }
                else
                {
                    Directory.CreateDirectory(LogPath);
                    sw = File.CreateText(totalPath);
                }
                sw.WriteLine(msg);
            }
            finally
            {
                if (sw != null)
                {
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
        }

        /// <summary>
        /// 一次性读完所有内容
        /// </summary>
        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);
                }
            }
        }



        /// <summary>
        /// 一次性读完所有内容
        /// </summary>
        public static void ReadOneLine()
        {
            string fileName = @"info.txt";
            string totalPath = Path.Combine(LogPath, fileName);

            if (File.Exists(totalPath))
            {
                using (StreamReader sr = new StreamReader(totalPath))
                {
                    string oneLine = sr.ReadLine();
                    while (oneLine != null)
                    {
                        Console.WriteLine(oneLine);
                        oneLine = sr.ReadLine();
                    }
                }
            }
        }

        /// <summary>
        /// 读大量数据
        /// </summary>
        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);
            //byData传进来的字节数组,用以接受FileStream对象中的数据,
            //第2个参数是字节数组中开始写入数据的位置,它通常是0,表示从数组的开端文件中向数组写数据,
            //最后一个参数规定从文件读多少字符.
            file.Read(byteData, 0, 100);
            Decoder d = Encoding.Default.GetDecoder();
            d.GetChars(byteData, 0, byteData.Length, charData, 0);
            Console.WriteLine(charData);
            file.Close();
        }

        /// <summary>
        /// 对文本进行复制
        /// </summary>
        public static void Copy()
        {
            string fileName = "info.txt";
            string totalPath = Path.Combine(LogPath, fileName);

            if (File.Exists(totalPath))
            {
                File.Copy(totalPath, totalPath.Replace("info.txt", "info_copy.txt"));
            }
        }

        /// <summary>
        /// 删除文本
        /// </summary>
        public static void Delete()
        {
            string fileName = "info.txt";
            string totalPath = Path.Combine(LogPath, fileName);

            if (File.Exists(totalPath))
            {
                File.Delete(totalPath);
            }
        }

        /// <summary>
        /// 使用递归方式找全部文件
        /// </summary>
        public static void RecursionShow()
        {
            string rootPath = @"E:\APICloud\";
            DirectoryInfo dirRoot = new DirectoryInfo(rootPath);
            List<FileInfo> fileInfoList = new List<FileInfo>();
            fileInfoList = GetFileByDir(dirRoot, fileInfoList);
        }

        private static List<FileInfo> GetFileByDir(DirectoryInfo dirParent, List<FileInfo> fileInfoList)
        {
            FileInfo[] fileArray = dirParent.GetFiles();
            if (fileArray != null && fileArray.Length > 0)
            {
                fileInfoList.AddRange(fileArray.ToList());
            }
            DirectoryInfo[] dirArray = dirParent.GetDirectories();
            if (dirArray != null && dirArray.Length > 0)
            {
                foreach (DirectoryInfo dir in dirArray)
                {
                    GetFileByDir(dir, fileInfoList);
                }
            }
            return fileInfoList;
        }
    }
}

.config

  <appSettings>
    <!--日志路径-->
    <add key="LogPath" value="E:\"/>
  </appSettings>

 

posted @ 2015-12-26 17:24  时代码农  阅读(188)  评论(0编辑  收藏  举报