您的位置:首页 > 编程语言 > C#

C#streamwriter写文件

2010-11-21 15:59 246 查看
using
System;
using
System.IO;

class
Test

{
public
static
void
Main()

{
string
path = @"c:/temp/MyTest.txt"
;
// This text is added only once to the file.

if
(!File.Exists(path))

{
// Create a file to write to.

using
(StreamWriter sw = File.CreateText(path))

{

sw.WriteLine("Hello"
);

sw.WriteLine("And"
);

sw.WriteLine("Welcome"
);

}

}

// This text is always added, making the file longer over time

// if it is not deleted.

using
(StreamWriter sw = File.AppendText(path))

{

sw.WriteLine("This"
);

sw.WriteLine("is Extra"
);

sw.WriteLine("Text"
);

}

// Open the file to read from.

using
(StreamReader sr = File.OpenText(path))

{
string
s = ""
;
while
((s = sr.ReadLine()) != null
)

{

Console.WriteLine(s);

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  path string file null c