| ASP.NET入门 |
|
作者:mike 文章来源:internet 点击数: 更新时间:2004-9-2 20:05:29  |
|
File.AppendText 方法 创建一个 StreamWriter,它将 UTF-8 编码文本追加到现有文件。
public static StreamWriter AppendText( string path );
参数 path 要向其中追加内容的文件的路径。 返回值 一个 StreamWriter,它将 UTF-8 编码文本追加到现有文件。
将文本追加到文件
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);
}
}
}
}
using System;
using System.IO;
class Test
{
public static void Main()
{
FileInfo fi = new FileInfo(@"c:\temp\MyTest.txt");
// This text is added only once to the file.
if (!fi.Exists)
{
//Create a file to write to.
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
// This text will always be added, making the file longer over time
// if it is not deleted.
using (StreamWriter sw = fi.AppendText())
{
sw.WriteLine("This");
sw.WriteLine("is Extra");
sw.WriteLine("Text");
}
//Open the file to read from.
using (StreamReader sr = fi.OpenText())
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}
=====================================
using System;
using System.IO;
public class AppendTextTest
{
public static void Main()
{
// Create a reference to a file, which might or might not exist.
// If it does not exist, it is not yet created.
FileInfo fi = new FileInfo("temp.txt");
// Create a writer, ready to add entries to the file.
StreamWriter sw = fi.AppendText();
sw.WriteLine("Add as many lines as you like...");
sw.WriteLine("Add another line to the output...");
sw.Flush();
sw.Close();
// Get the information out of the file and display it.
// Remember that the file might have other lines if it already existed.
StreamReader sr = new StreamReader(fi.OpenRead());
while (sr.Peek() != -1)
Console.WriteLine( sr.ReadLine() );
}
}
<< 上一页 [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] 下一页 |
| 文章录入:admin 责任编辑:admin |
|
上一篇文章: asp.net 1.1 安装手册
下一篇文章: 关于.net的一些基本的东西 |
| 【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |