Practice Exercises C# Sharp - Learn to program with performing exercises C# Sharp

Practice Exercises C# Sharp

Learn to program with performing exercises C# Sharp

File splitter - Practice Exercises C# Sharp
Practice Exercises C# Sharp4,85320940

File splitter - Practice Exercises C# Sharp


Lesson 8:

File management


Exercise 8.28:

File splitter


Objetive:

Create a program to split a file (of any kind) into pieces of a certain size. ir must receive the name of the file and the size as parameters. For example, it might be used by writing:

split myFile.exe 2000

If the file "myFile.exe" is 4500 bytes long, that command would produce a file named "myFile.exe.001" 2000 bytes long, another called "myFile.exe.002" 2000 bytes long, and another named "myFile.exe.003" 500 bytes long.


Source Code:


using System;
using System.IO;
public class splitFile
{
    public static void Main(string[] args)
    {

        FileStream myFile;
        FileStream myNewFile;

        string nameFile;
        int BUFFER_SIZE;
        int amountRead;
        int count = 1;

        if (args.Length == 2)
        {
            nameFile = args[0];
            BUFFER_SIZE = Convert.ToInt32(args[1]);
            byte[] data = new byte[BUFFER_SIZE];

            try
            {
                myFile = File.OpenRead(nameFile);
                do
                {
                    amountRead = myFile.Read(data, 0, BUFFER_SIZE);
                    myNewFile = File.Create(nameFile + count.ToString("000"));
                    myNewFile.Write(data, 0, amountRead);
                    count++;
                    myNewFile.Close();

                }
                while (amountRead == BUFFER_SIZE);
                myFile.Close();
            }
            catch (Exception fileError)
            {
                Console.WriteLine("ERROR has ocurred while executing: " + fileError.Message);
            }
        }
        else
        {
            Console.WriteLine("The parameters are incorrects");
            Console.WriteLine("usage: splitfile namefile sizeinbytes");
        }
    }
}
Exercisey 8.28

Share Now:







Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.