zoukankan      html  css  js  c++  java
  • C# txt 文件分割

    <appSettings>
    <!--是否开启大文件分隔策略-->
    <add key="BigFile.Split" value="true"/>
    <!--当文件大于这个配置项时就执行文件分隔,单位:GB -->
    <add key="BigFile.SplitMinFileSizeGB" value="10" />
    <!--当文件大于这个配置项时就执行文件分隔,单位:MB -->
    <add key="BigFile.SplitMinFileSizeMB" value="10" />
    <!--当执行文件分割时,每个分隔出来的文件大小,单位:MB -->
    <add key="BigFile.SplitFileSize" value="100"/>
    <add key="BigFile.FilePath" value="d:igfilesms6.api.yiche.com20191023.txt"/>
    <add key="BigFile.FileSilitPathFormate" value="\172.x1.xx.xx文件拷贝liulongFTPxx2016-04-07x_20160407{0}.txt"/>
    </appSettings>

        public class SplitFileStrategy2
        {
            public const byte Newline = 10;
            public const byte Return = 13;
            public void Split()
            {
                string file = ConfigurationManager.AppSettings.Get("BigFile.FilePath");
    
                int splitMinFileSizeMB = Convert.ToInt32(ConfigurationManager.AppSettings.Get("BigFile.SplitMinFileSizeMB")) * 1024 * 1204;
                int splitFileSize = Convert.ToInt32(ConfigurationManager.AppSettings.Get("BigFile.SplitFileSize")) * 1024 * 1024;
                FileInfo fileInfo = new FileInfo(file);
    
                if (fileInfo.Length > splitMinFileSizeMB)
                {
                    Console.WriteLine("判定结果:需要分隔文件!");
                }
                else
                {
                    Console.WriteLine("判定结果:不需要分隔文件!");
                    Console.ReadKey();
                    return;
                }
                int steps = (int)(fileInfo.Length / splitFileSize);
                var length = fileInfo.Length;
                using (FileStream fs = fileInfo.Open(FileMode.Open, FileAccess.Read))
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        int couter = 1;
                        bool isReadingComplete = false;
                        while (!isReadingComplete)
                        {
                            string filePath = $"{fileInfo.FullName}.{couter}{fileInfo.Extension}";
                            Console.WriteLine("开始读取文件【{1}】:{0}", filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                            byte[] input = br.ReadBytes(splitFileSize);
                            var newLine = GetLineFeed(br);
                            using (FileStream writeFs = new FileStream(filePath, FileMode.Create))
                            {
                                using (BinaryWriter bw = new BinaryWriter(writeFs))
                                {
                                    bw.Write(input);
                                    bw.Write(newLine);
                                }
                            }
                            isReadingComplete = br.BaseStream.Position >= length;
                            if (!isReadingComplete)
                            {
                                couter += 1;
                            }
                            Console.WriteLine("完成读取文件【{1}】:{0}", filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                        }
                    }
                }
                Console.WriteLine("分隔完成,请按下任意键结束操作。。。");
                Console.ReadKey();
            }
    
            /// <summary>
            /// 读取至换行
    
            /// </summary>
            /// <returns></returns>
            public byte[] GetLineFeed(BinaryReader br)
            {
                var list = new List<byte>();
                while (true)
                {
                    try
                    {
                        var currentByte = br.ReadByte();
                        list.Add(currentByte);
                        //小端存储
                        if (BitConverter.IsLittleEndian)
                        {
                            if (currentByte == Return)
                            {
                                currentByte = br.ReadByte();
                                list.Add(currentByte);
                                if (currentByte == Newline)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            if (currentByte == Newline)
                            {
                                currentByte = br.ReadByte();
                                list.Add(currentByte);
                                if (currentByte == Return)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    catch (EndOfStreamException ex)
                    {
                        break;
                    }
    
                }
                return list.ToArray();
    
            }
        }
    

      

  • 相关阅读:
    安卓获取双IMEI
    NodeJS异步、同步 创建多层文件夹
    Winfrom 控件名称缩写
    Unobtrusive Ajax
    ID 为 17608的进程当前未运行
    欢迎
    路由
    VS快捷键
    Test
    并查集与带权并查集---由浅入深
  • 原文地址:https://www.cnblogs.com/xiyoujiyjy/p/11736632.html
Copyright © 2011-2022 走看看