Simple text file operations in C #. Example

Source: Internet
Author: User

C-sharp provides a file class which is used in manipulating text files. the file class is within the system namespace. also we can use the streamreader and streamwriter classes, which are within the system. io, namespace for reading from and writing to a text file. in this article we'll see examples of creating a text file, reading contents of a text file and appending lines to a text file.

1. Creating a text file

For creating text file we use the createtext method of the file class. the createtext methods takes in the path of the file to be created as an argument. it creates a file in the specified path and returns a streamwriter object which can be used to write contents to the file.

Example


Public class fileclass
{
Public static void main ()
{
Writetofile ();
}
Static void writetofile ()
{
Streamwriter SW;
Sw = file. createtext ("C :\\ mytextfile.txt ");
Sw. writeline ("God is greatest of them all ");
Sw. writeline ("this is second line ");
Sw. Close ();
Console. writeline ("File Created sucacessfully ");
}
}

2. Reading contents of a text file

For reading the contents of a text file we use the opentext method of the file class. the opentext methods takes in the path of the file to be opened as an argument. it opens the specified file and returns a streamreader object which can be used to read the contents of the file.

Example


Public class fileclass
{
Public static void main ()
{
Readfromfile ("C: \ mytextfile.txt ");
}
Static void readfromfile (string filename)
{
Streamreader SR;
String S;
Sr = file. opentext (filename );
S = Sr. Readline ();
While (s! = NULL)
{
Console. writeline (s );
S = Sr. Readline ();
}
Sr. Close ();
}
}

3. appending content to a text file

For appending content to a text file we use the appendtext method of the file class. the appendtext methods takes in the path of the file to which the contents to be appended as an argument. it opens the file in the specified path and returns a streamwriter object which can be used to append contents to the file.

Example

  
public class fileclass
{
Public static void main ()
{< br> appendtofile ();
}< br> static void appendtofile ()
{< br> streamwriter SW;
Sw = file. appendtext ("C :\\ mytextfile.txt");
SW. writeline ("this line is appended");
SW. close ();
console. writeline ("text appended successfully");
}< br>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.