如何将文本文件拆分为多个文本文件? [英] How to Split text file into multiple text files?

查看:239
本文介绍了如何将文本文件拆分为多个文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专家

我正在寻求帮助,以根据文件的大小将一个大型文本文件拆分为多个文本文件.

I'm looking for help to split a large text file into multiple text files based on size of the File.

有人可以帮我吗?

推荐答案

有关您的要求的更多信息会有所帮助.当您说基于文件的大小"时,您到底是什么意思?如果您希望每个文件最大为某个大小,则可以执行以下操作:

Some more information about your requirements would help. What exactly do you mean when you say "based on size of the File"? If you wanted each file to be a maximum of a certain size you could do something like:

字符串 sourceFileName = @" ; C:\ VS2005 SP1.exe;

string sourceFileName = @"C:\VS2005 SP1.exe";

字符串 destFileLocation = @" ; C:\&;

string destFileLocation = @"C:\";

int index = 0;

int index = 0;

maxFileSize = 52428800;

long maxFileSize = 52428800;

byte [] buffer = new 字节 [65536];

byte[] buffer = new byte[65536];

使用 ( source = 文件 .OpenRead(sourceFileName))

using (Stream source = File.OpenRead(sourceFileName))

{

(source.Position<.source.Length)

    while (source.Position < source.Length)

        index++;

//创建一个新的子文件,并读入t

        // Create a new sub File, and read into t

字符串 newFileName = 路径 .Combine(destFileLocation, .GetFileNameWithoutExtension(sourceFileName));

        string newFileName = Path.Combine(destFileLocation, Path.GetFileNameWithoutExtension(sourceFileName));

路径 .GetExtension(sourceFileName);

        newFileName += index.ToString() + Path.GetExtension(sourceFileName);

使用 ( 目标= .OpenWrite(newFileName))

        using (Stream destination = File.OpenWrite(newFileName))

{

while (destination.Position< maxFileSize)

            while (destination.Position < maxFileSize)

//确定要读取多少字节

                // Work out how many bytes to read

int 字节= source.Read(buffer,0, ( int ) 数学 .Min(maxFileSize,buffer.Length));

                int bytes = source.Read(buffer, 0, (int) Math.Min(maxFileSize, buffer.Length));

                destination.Write(buffer, 0, bytes);

//我们在文件的末尾吗?

                // Are we at the end of the file?

如果 (字节< 数学 .Min(maxFileSize,buffer.Length))

                if (bytes < Math.Min(maxFileSize, buffer.Length))

break ;

                    break;

}


这篇关于如何将文本文件拆分为多个文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆