如何向现有文件中添加文本
usingSystem;
usingSystem.IO;
classTest
{
publicstaticvoidMain()
{
//CreateaninstanceofStreamWritertowritetexttoafile.
//TheusingstatementalsoclosestheStreamWriter.
using(StreamWritersw=newStreamWriter("TestFile.txt"))
{
//Addsometexttothefile.
sw.Write("Thisisthe");
sw.WriteLine("headerforthefile.");
sw.WriteLine("-------------------");
//Arbitraryobjectscanalsobewrittentothefile.
sw.Write("Thedateis:");
sw.WriteLine(DateTime.Now);
}
}
}
如何创建一个新文本文件并向其中写入一个字符串。<link keywords="Overload:System.IO.FileSystem.WriteAllText">方法可提供类似的功能。
usingSystem;
usingSystem.IO;
publicclassTextToFile
{
privateconststringFILE_NAME="MyFile.txt";
publicstaticvoidMain(String[]args)
{
if(File.Exists(FILE_NAME))
{
Console.WriteLine("{0}alreadyexists.",FILE_NAME);
return;
}
using(StreamWritersw=File.CreateText(FILE_NAME))
{
sw.WriteLine("Thisismyfile.");
sw.WriteLine("Icanwriteints{0}orfloats{1},andsoon.",
1,4.2);
sw.Close();
}
}
}
如何从文本文件中读取文本
usingSystem;
usingSystem.IO;
classTest
{
publicstaticvoidMain()
{
try
{
//CreateaninstanceofStreamReadertoreadfromafile.
//TheusingstatementalsoclosestheStreamReader.
using(StreamReadersr=newStreamReader("TestFile.txt"))
{
Stringline;
//Readanddisplaylinesfromthefileuntiltheendof
//thefileisreached.
while((line=sr.ReadLine())!=null)
{
Console.WriteLine(line);
}
}
}
catch(Exceptione)
{
//Lettheuserknowwhatwentwrong.
Console.WriteLine("Thefilecouldnotberead:");
Console.WriteLine(e.Message);
}
}
}
在检测到文件结尾时向您发出通知。通过使用<link keywords="Overload:System.IO.File.ReadAll">或<link keywords="Overload:System.IO.File.ReadAllText">方法也可以实现此功能。
usingSystem;
usingSystem.IO;
publicclassTextFromFile
{
privateconststringFILE_NAME="MyFile.txt";
publicstaticvoidMain(String[]args)
{
if(!File.Exists(FILE_NAME))
{
Console.WriteLine("{0}doesnotexist.",FILE_NAME);
return;
}
using(StreamReadersr=File.OpenText(FILE_NAME))
{
Stringinput;
while((input=sr.ReadLine())!=null)
{
Console.WriteLine(input);
}
Console.WriteLine("Theendofthestreamhasbeenreached.");
sr.Close();
}
}
分享到:
相关推荐
基于C#语言(WindowForm窗体应用程序示例)实现文本文件读取和写入方法;文件的删除、移动和复制;对文件中的目录文件进行读取显示与删除等操作
一个很不错的C#的杨辉三角的例子
c# 对文本文档操作 写入 读取 读取指定行 删除指定行 查询 以指定条件读取txt文件内容,读取结果以DataTable
C#读写文本文件操作示例,可以打开TXT格式的文件,写入TXT文件,可能编码有些问题,在打开中文文件的时候有乱码。
C# 文本文档学习综合示例对文件的读娶等等的基础操作
文本读取 文本写入 文本操作 帮助类 WriteFiles
c# 文件上传,下载,压缩rar,zip,解压rar,zip,文件流读取,excel文件读取上传等操作总结
C#对文本文件的操作入门源码 把listview中的内容写入到文本文件 从文本文件读取内容到listview中
c#对文件和文本的操作,追加文档,删除,创建,格式化等等,附带源代码
为此,特地采用C#实现从文本文件中提取出数据(如浮点型),可以自动适应单个或多个空白符(空格、\t、\r、\n等)或逗号作为分隔符,可以实现C++语言中“>>”操作符的能力。 JunliClassLib.cs为函数源码,Program.cs为...
案例1:循环语言的学习-砝码程序验证 案例2:...--消去字符串空格 案例7:委托的使用方法实例---加减大小比较 案例8:用C#索引器实现文本文件的倒叙输入 案例9:C#文本文件操作实例---杨辉三角形写入读出
C#操作Word——C#向Word文档中写入文字
第一章C# Windows编程基础 实例1 循环语言的学习-砝码程序验证 实例2 随机数的案例-洗牌程序 实例3 通过实例来剖析C#继承机制 实例4 递归与全局变量的案例1-打靶... 实例9 C#文本文件操作实例---杨辉三角形写入读出
1、C#csv或者txt文本文件读操作 2、C#进行dat读操作 3、C#进行dat写操作 4、提供csv格式测试文件
(外加图片插入功能)知道最近C#课程设计有这个题,提前完成,跟大家分享一下,特别是浙工大的同学 按照实验要求,设计一个多文档文本编辑器,能够实现对文本的编辑、排版、打印等。程序启动时要有密码输入提示,...
使用C#编写的文本编辑器,能够打开RTF格式的文档,对文档中的内容进行颜色变换,字体加粗,倾斜,下划线等操作。
主要介绍了C#保存listbox中数据到文本文件的方法,涉及C#操作listbox数据的相关技巧,需要的朋友可以参考下
一个简单的源码文件,本机调试可用。实现对txt文本文档的分割效果。本人刚接触C#,高手勿入。与初学者交流学习。
C#读取BIN文件内容,获取其中数据并显示。用于对BIN文件的操作。
C#操作Txt文件源码