C# File WriteAllLines(String, IEnumerable, Encoding)
Description
Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.
Syntax
public static void WriteAllLines(
string path,// w ww .j a va 2 s . c o m
IEnumerable<string> contents,
Encoding encoding
)
Parameters
path
- The file to write to.contents
- The lines to write to the file.encoding
- The character encoding to use.
Example
/*from ww w.ja v a2 s . com*/
using System;
using System.IO;
using System.Linq;
using System.Text;
class Program
{
static string dataPath = @"c:\temp\data.txt";
static void Main(string[] args)
{
var da = from line in File.ReadLines(dataPath)
where (line.StartsWith("a"))
select line;
File.WriteAllLines(@"C:\temp\new.txt", da, Encoding.UTF8);
}
}