C# File WriteAllLines(String, String[], Encoding)

Description

File WriteAllLines(String, String[], Encoding) Creates a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.

Syntax

File.WriteAllLines(String, String[], Encoding) has the following syntax.


public static void WriteAllLines(
  string path,/*from w  w w.  j a v a 2 s  .  c o m*/
  string[] contents,
  Encoding encoding
)

Parameters

File.WriteAllLines(String, String[], Encoding) has the following parameters.

  • path - The file to write to.
  • contents - The string array to write to the file.
  • encoding - An Encoding object that represents the character encoding applied to the string array.

Returns

File.WriteAllLines(String, String[], Encoding) method returns

Example

The following code example demonstrates the use of the WriteAllLines method to write text to a file.


using System;/*from  www .ja va 2  s .  c o  m*/
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        string[] createText = { "Hello", "And", "java2s.com" };
        File.WriteAllLines(path, createText, Encoding.UTF8);


        string[] readText = File.ReadAllLines(path, Encoding.UTF8);
        foreach (string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter