C # Create text file write read implementation code _c# tutorial

Source: Internet
Author: User

The first time you run:

Second run time:


Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.IO;
Namespace file operations
{
Class Program
{
static void Main (string[] args)
{
To create a text file, it's best to judge
StreamWriter SW;
if (! File.exists ("Templog.txt"))
{
Create a new text file if it does not exist, and write some content
SW = File.createtext ("Templog.txt");
Sw. Write ("First word");
Sw. WriteLine ("Follow the boss's.");
Sw. WriteLine ("Current date is:");
Sw. WriteLine (DateTime.Now);
}
Else
{
Add some text content if it exists
SW = File.appendtext ("Templog.txt");
for (int i = 0; i < i++)
{
Sw. WriteLine ("can output {0}" as usual to the screen, I);
}
}
Sw. Close ();
Create a Reader
StreamReader sr = new StreamReader ("Templog.txt");
Read at a one-time
Console.WriteLine (Sr. ReadToEnd ());
Console.ReadLine ();
}
}
}

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.