File splitter
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that divides text or binary files into parts of 5 Kb each. You can use the FileStream
object to read files and write the different parts of it.
Use the name-00 format to call each part of the file.
Input
Output
Solution
using System.IO;
public class FileSplitter
{
public static void Main(string[] args)
{
int BUFFER_SIZE = 5 * 1024;
byte[] data = new byte[BUFFER_SIZE];
int amountRead;
int count = 1;
string inputNameFile = "app";
string ext = ".exe";
using (FileStream inputFile = File.OpenRead(inputNameFile + ext))
{
do
{
amountRead = inputFile.Read(data, 0, BUFFER_SIZE);
if (amountRead > 0)
{
using (FileStream myNewFile = File.Create(inputNameFile + "-" + count.ToString("00")))
{
myNewFile.Write(data, 0, amountRead);
count++;
}
}
}
while (amountRead == BUFFER_SIZE);
}
}
}