+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 16

Thread: Reading and writing files using File methods

  1. #1
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,379

    Reading and writing files using File methods

    Hello members! This tutorial is a bit nostalgic to me. Here is one of the few reasons why I am into .NET programming so much, instead of Java or C++ for example. I want to show you the File class, which is a small thing but makes my day nice.

    We need a sample file...

    For a start, we need some sample file to read it. I created a new console application. Then I selected the Program.cs file in the Solution Explorer panel, and switched to Properties panel. There is a property called Copy to Output Directory. I changed it to have a copy of this file in the Bin\Release\ folder. Viola, a sample file to read it, right up.



    Reading and writing with File class

    Let's start with some simple code, the goal is to read a file. After writing "File" press Ctrl+. and click to import the System.IO namespace. Then you just need one line of code, which one, depends on your needs.

    Code:
    byte[] asBytes File.ReadAllBytes("Program.cs");

    string asString File.ReadAllText("Program.cs");

    string[] asLines File.ReadAllLines("Program.cs"); 
    Displaying the file contents

    Here are screenshots of looking up the variables that contain what was read. Look at them, and decide which one of them is useful for you at the moment.

    Reading and writing files using File methods-bytes.jpgReading and writing files using File methods-string.jpgReading and writing files using File methods-lines.jpg

    Writing data back to files

    This is again what I like in .NET the most. All writing can be done in one line of code. Unless those files weight in gigabytes, your pc will not run out of memory. You can read for example setting files, or code (.cs) files this way without worrying that it will eat up your memory.

    Depending on which variable or data type you want to write, you can pick one of the lines.

    Code:
    File.WriteAllBytes("Saved as bytes.cs", asBytes);
    
    File.WriteAllText("Saved as string.cs", asString);
    
    File.WriteAllLines("Saved as lines.cs", asLines);
    Leave comments and enjoy your day

    I hope this tutorial will be helpful or at least make your day a little bit less of writing code and more of enjoying your day. Comments and +rep are always appreciated.
    Bijgevoegde miniaturen Reading and writing files using File methods-control-dot.jpg   Reading and writing files using File methods-copy-output-directory.jpg  
    Bijgevoegde bestanden
    Last edited by ArekBulski; 09-03-2009 at 05:41 AM. Reason: added the solution zip

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,750
    Blog Entries
    97

    Re: Reading and writing files using File methods

    Short and sweet. +rep

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    13,155
    Blog Entries
    59

    Re: Reading and writing files using File methods

    Nice +rep
    CodeCall Blog | CodeCall Wiki
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #4
    Learning Programmer sp3tsnaz is an unknown quantity at this point sp3tsnaz's Avatar
    Join Date
    Sep 2009
    Posts
    40

    Re: Reading and writing files using File methods

    now what is +rep ?

  5. #5
    Learning Programmer sp3tsnaz is an unknown quantity at this point sp3tsnaz's Avatar
    Join Date
    Sep 2009
    Posts
    40

    Re: Reading and writing files using File methods

    and nice tuto again 11/10 (Y)

  6. #6
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    13,155
    Blog Entries
    59

    Re: Reading and writing files using File methods

    +rep is what you get when someone clicks on the scales icon. It's a rough measure of your contribution to the forum.
    CodeCall Blog | CodeCall Wiki
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #7
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,379

    Re: Reading and writing files using File methods

    As Winged said. Scales icon is in the top-right corner of a post.

  8. #8
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,379

    Re: Reading and writing files using File methods

    Looool...sp3tsnaz, you gave me negative -rep. You must have selected "I disapprove" option. Thanks.

  9. #9
    Learning Programmer sp3tsnaz is an unknown quantity at this point sp3tsnaz's Avatar
    Join Date
    Sep 2009
    Posts
    40

    Re: Reading and writing files using File methods

    aww sorry man :| im new to this forum

  10. #10
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,379

    Re: Reading and writing files using File methods

    I don't mind it ahem this time.

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. File Input and Output
    By chili5 in forum Java Tutorials
    Replies: 4
    Last Post: 01-13-2010, 03:30 PM
  2. Reading and writing files using FileStream class
    By ArekBulski in forum CSharp Tutorials
    Replies: 4
    Last Post: 09-06-2009, 02:53 AM
  3. C# Tutorial: Writing Text Files
    By Xav in forum CSharp Tutorials
    Replies: 46
    Last Post: 07-28-2009, 05:18 AM
  4. Replies: 2
    Last Post: 07-16-2009, 11:48 AM
  5. Replies: 1
    Last Post: 12-05-2008, 01:42 PM