C# 读取100M文本文件并分割写入新文件 - 代码示例
{"title":"C# 读取100M文本文件并分割写入新文件 - 代码示例","description":"使用C#代码读取100M大小的a.txt文本文件,并以换行符分割,将内容写入到新的文件b.txt。本文提供详细代码示例,并解释代码原理。","keywords":"C#, 文件读取, 文件写入, StreamReader, StreamWriter, 文本文件, 大文件, 代码示例","content":"可以使用以下代码来读取和写入文件:\n\ncsharp\nusing System;\nusing System.IO;\n\nclass Program\n{\n static void Main()\n {\n string inputFile = \"a.txt\";\n string outputFile = \"b.txt\";\n\n using (StreamReader reader = new StreamReader(inputFile))\n using (StreamWriter writer = new StreamWriter(outputFile))\n {\n string line;\n while ((line = reader.ReadLine()) != null)\n {\n writer.WriteLine(line);\n }\n }\n\n Console.WriteLine(\"文件写入完成!\");\n }\n}\n\n\n上述代码使用 StreamReader 读取输入文件的内容,并使用 StreamWriter 将每行内容写入输出文件。\n\n该代码适用于读取任意大小的文本文件,并且可以有效地处理大文件。\n\n注意:\n\n* 确保输入文件 a.txt 和输出文件 b.txt 存在于同一目录下。\n* 可以根据需要修改代码中的文件路径和文件名。
原文地址: http://www.cveoy.top/t/topic/pJOz 著作权归作者所有。请勿转载和采集!