methodological_instructions (10)

  • doc
  • 02.05.2020
Публикация на сайте для учителей

Публикация педагогических разработок

Бесплатное участие. Свидетельство автора сразу.
Мгновенные 10 документов в портфолио.

Иконка файла материала methodological_instructions (10).doc

Methodological Instructions

Theme: Work with files

Aim: Use of files to read and write information

Assessment criteria

Knowledge

·     Know how to read from text file

·     Know how to write to text file

 

Understanding

·     Describes the process of reading from text file

·     Describes the process of writing to text file

 

Application

·     Write code for reading from text file

·     Write code for writing to text file

 

Language objectives

Student is able to describe the process of reading and writing the information from and to file.

For instance, the piece of text is being read out from file. So, text file has to be opened and then … .

Also, student can justify the purpose of passing information to and from file

Vocabulary and terminology specific to the subject:

Text file, pass data, open file for reading, open file for writing

Useful expressions for dialogs and letters:

Well, the variable of string type is passed to text file for … .

For example, reading every line of file is   … .

Obviously, the text files are used for the wide variety of cases like  … .

 

Theory

Read from text file

This example reads the contents of a text file by using the static methods ReadAllTextand ReadAllLines from the System.IO.File class.

For an example that uses StreamReader, see How to: Read a Text File One Line at a Time.

 

Example

C#

class ReadFromFile
{
    static void Main()
    {
        // The files used in this example are created in the topic
        // How to: Write to a Text File. You can change the path and
        // file name to substitute text files of your own.
 
        // Example #1
        // Read the file as one string.
        string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");
 
        // Display the file contents to the console. Variable text is a string.
        System.Console.WriteLine("Contents of WriteText.txt = {0}", text);
 
        // Example #2
        // Read each line of the file into a string array. Each element
        // of the array is one line of the file.
        string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");
 
        // Display the file contents by using a foreach loop.
        System.Console.WriteLine("Contents of WriteLines2.txt = ");
        foreach (string line in lines)
        {
            // Use a tab to indent each line of the file.
            Console.WriteLine("\t" + line);
        }
 
        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
 

 

Write to a Text File

These examples show various ways to write text to a file. The first two examples use static convenience methods on the System.IO.File class to write each element of any IEnumerable<string> and a string to a text file. Example 3 shows how to add text to a file when you have to process each line individually as you write to the file. Examples 1-3 overwrite all existing content in the file, but example 4 shows you how to append text to an existing file.

 

These examples all write string literals to files. If you want to format text written to a file, use the Format method or C# string interpolation feature.

 

Example

class WriteTextFile

{

    static void Main()

    {

        // These examples assume a "C:\Users\Public\TestFolder" folder on your machine.

        // You can modify the path if necessary.     

        // Example #1: Write an array of strings to a file.

        // Create a string array that consists of three lines.

        string[] lines = { "First line", "Second line", "Third line" };

        // WriteAllLines creates a file, writes a collection of strings to the file,

        // and then closes the file.  You do NOT need to call Flush() or Close().

        System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);

 

 

        // Example #2: Write one string to a text file.

        string text = "A class is the most powerful data type in C#. Like a structure, " +

                       "a class defines the data and behavior of the data type. ";

        // WriteAllText creates a file, writes the specified string to the file,

        // and then closes the file.    You do NOT need to call Flush() or Close().

        System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);

 

        // Example #3: Write only some strings in an array to a file.

        // The using statement automatically flushes AND CLOSES the stream and calls

        // IDisposable.Dispose on the stream object.

        // NOTE: do not use FileStream for text files because it writes bytes, but StreamWriter

        // encodes the output as text.

        using (System.IO.StreamWriter file =

            new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))

        {

            foreach (string line in lines)

            {

                // If the line doesn't contain the word 'Second', write the line to the file.

                if (!line.Contains("Second"))

                {

                    file.WriteLine(line);

                }

            }

        }

 

        // Example #4: Append new text to an existing file.

        // The using statement automatically flushes AND CLOSES the stream and calls

        // IDisposable.Dispose on the stream object.

        using (System.IO.StreamWriter file =

            new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt", true))

        {

            file.WriteLine("Fourth line");

        }

    }

}

//   A class is the most powerful data type in C#. Like a structure, a class defines the data and behavior of the data type.

    

 

 

П. Questions for self-assessment.

How to read from file?

How to write to file?

 

Visual Aids and Materials links.

  1. Slides

2.      https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-read-from-a-text-file

3.      https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file

 

 

 

 

Students' Practical Activities:

Students must be able:

·     To write program for reading from text file

·     To write program for writing to text file

 


Скачано с www.znanio.ru

Посмотрите также