C# handling files - What classes/methods to choose?

Discussion in 'Programmer's Corner' started by atferrari, Dec 1, 2008.

  1. atferrari

    Thread Starter AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    I am learning C# by myself with no previous experience other than Basic (yes, the old one) and Assembler for micros. Not conversant in C, C++ or Java either. So please don't refer to them like most tutorials do.

    Since the initial application I have in mind is to be file-handling intensive I want this straight from the beginning. There are so many options for working with files (creating / erasing / seeking / appending / erasing / deleting / listing and the like) that I find the list of objects/classes/methods somewhat overwhelming.

    I have the feeling that there is not a "one fits all" solution but I would like to avoid wasting time trying to learn every single way, to find later that it was not worth the pain.

    Honestly, every time I try to adavance on the subject, something new shows up adding to my confusion which increases even more with someone else saying:"this is the right thing to do!".

    As you could see I am asking for the criteria to follow, not code.

    Just in case, please keep all referred to Console output/input. I will work out my way for a graphic interface at a later moment.

    Help is needed and appreciated. Gracias.
     
  2. nemaroller

    New Member

    Dec 1, 2008
    3
    0
    Well, there are many options because there are many different ways to access a file, in different formats, with different lock mechanisms, and for different reasons.

    That said, the lowest generic component is simply the FileStream Read and Write methods which either Read or Write an array of bytes from or to the disk. Of course, you need to have an array of bytes to write from.

    Some people only need to read/write text, and care less about working with bytes. For that reason, a StreamReader and StreamWriter exists as a sort of extender. You can then use a StreamReader.ReadAllText method to read all text from the file or StreamReader.ReadLine to read just the next line of text from the file.

    Disgread these classes:
    StringReader/StringWriter - reads/writes text to a string, not a file - therefore mostly useless to you.

    TextReader/TextWriter - an abstract class you cannot instantiate (for your purposes 99.9995% of the time you can just pretend this class doesn't even exist).

    File.WriteAllText, File.ReadAllText - This class offers only two methods - Read all the text, or write all text. Useful if you want to read all information from a file, but what if the file is 80mb's long? With this class, you would have to read all 80mb's of text just to get the first line of text. Further, you can't specify sharing permissions (if other applications can use the file).

    You need to decide amonst these:

    BinaryReader/BinaryWriter
    Read/writes bytes to a stream (like a file stream). Allows you to pass in a integer, long, boolean, string, character, double, etc to its write and read methods.

    StreamReader/StreamWriter
    Read/writes text to a stream (like a file stream) in a particular byte encoding. For instance, 'Greetings, dear sir' may be represented by a different number of bytes depending on the encoding used - with StreamReader/Writer, you don't worry about that, you simply
    1) set the encoding format (if you don't set it, it defaults to UTF8)
    2) give the Write method some text and this class writes the proper sequence of bytes which represent that text.

    BinaryReader/BinaryWriter can also write text, whereas StreamReader/StreamWriter cannot write byte data, only text.

    If you're reading or writing JUST text, you can simply use StreamReader and StreamWriter. If you need to store image data or more data patterns you will need to use BinaryReader/BinaryWriter or the underlying Stream's Write method.

    using
    System;
    using System.Text;
    using System.IO;
    namespace ConsoleApplication2
    {

    class Program
    {

    static void Main(string[] args)
    {

    Program p = new Program();
    p.DoStreamWriter();
    p.DoBytes();
    p.DoBytesWithBinaryWriter();
    }
    // Demonstrate StreamWriter stream wrapper class
    void DoStreamWriter()
    {

    using (FileStream fs = new FileStream(@"C:\testStreamWriter.txt", FileMode.Create, FileAccess.Write))
    using (StreamWriter sw = new StreamWriter(fs))
    {
    sw.Write(​
    "This is some text in UTF-8 byte encoding.");
    }
    }

    // Demonstrate BinaryWriter stream wrapper class
    void DoBytesWithBinaryWriter()
    {

    using (FileStream fs = new FileStream(@"C:\testBinaryWriter.txt", FileMode.Create, FileAccess.Write))
    using (BinaryWriter bw = new BinaryWriter(fs))
    {

    int someValue = 45932;
    //write the byte value of someValue (a .NET integer is 4 bytes (32-bits) long).
    bw.Write(someValue);

    }
    }

    // Demonstrate use of stream class methods (no wrapper class)
    void DoBytes()
    {

    FileStream fs;
    using (fs = new FileStream(@"C:\testRawStreamMethod.txt", FileMode.Create, FileAccess.Write))
    {

    byte[] bytes;
    //get the byte representation of the 'this is some text in UTF-8 byte encoding.'
    bytes = System.Text.UTF8Encoding.UTF8.GetBytes("This is some text in UTF-8 byte encoding.");
    //write those bytes to file
    fs.Write(bytes, 0, bytes.Length);
    fs.Close();
    //create another file
    fs = new FileStream(@"C:\testRawStreamMethod.gif", FileMode.Create, FileAccess.Write);
    //create a byte array representation of a right-arrow gif.
    byte[] imageBytes = new byte[]{71,73,70,56,57,97,15,0,12,0,196,0,0,37,68,104,251 ,
    251,251,21,59,101,249,249,250,206,207,211,26,62,102,84,101,123,90, 105,127,108,119 ,137,161,165,174,178,180,187,131,139,152,120,130, 145,8,54,102,52,78,108,16,57,100,234,235,236,95,109,128,139,145, 157,70,90,116,230,230,232,36,67,104,12,55,101,186,189,194,255,255, 255,0,51,102,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,33,249,4,0,0,0,0,0,44, 0,0,0,0,15,0,12,0,0,5,52,32,38,26,201,32,158,232,153,53,17,148,190, 89,92,73,47,26,199,214,113,213,216,125,23,140,154,239,6,16,250,44, 8,138,49,230,216,241,98,130,69,128,39,122,76,8,212,147,34,203,77, 133,0 ,0 ,59};
    //write those bytes to file
    fs.Write(imageBytes, 0, imageBytes.Length);
    fs.Close();
    fs =
    new FileStream(@"C:\testtestRawStreamIntBytes.dat", FileMode.Create, FileAccess.Write);
    int someValue = 45932;
    byte[] numericBytes = System.BitConverter.GetBytes(someValue);
    fs.Write(numericBytes,0, numericBytes.Length);
    fs.Close();
    }
    }
    }
    }
     
  3. atferrari

    Thread Starter AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    Hola nemaroller,

    Thanks for the clear explanation!

    Sure there will be more doubts but for the moment it is enough to go ahead.
     
  4. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
  5. atferrari

    Thread Starter AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    I said already I am not C++ conversant. Thanks anyway.
     
Loading...