using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace StreamWrite
{
class Program
{
static void Main(string[] args)
{
try
{
FileStream aFile = newFileStream("Log.txt", FileMode.OpenOrCreate);
StreamWriter sw = newStreamWriter(aFile);
bool truth = true;
// Write data to file.
sw.WriteLine("Helloto you.");
sw.WriteLine("It isnow {0} and things are looking good.",
DateTime.Now.ToLongDateString());
sw.Write("More thanthat,");
sw.Write(" it's {0}that C# is fun.", truth);
sw.Close();
}
catch (IOException ex)
{
Console.WriteLine("An IOException has been thrown!");
Console.WriteLine(ex.ToString());
Console.ReadLine();
return;
}
}
}
}
读取文件,这里介绍StreamReader对象
static void Main(string[] args)
{
string strLine;
try
{
FileStream aFile = newFileStream("Log.txt",FileMode.Open);
StreamReader sr = newStreamReader(aFile);
strLine = sr.ReadLine();
//Read data in line by line 这个兄台看的懂吧~一行一行的读取
while(strLine != null)
{
Console.WriteLine(strLine);
Line = sr.ReadLine();
}
sr.Close();
}catch (IOException ex)
{
Console.WriteLine("An IOException hasbeen thrown!");
Console.WriteLine(ex.ToString());
Console.ReadLine();
return;
}
}
读写文件
最新推荐文章于 2022-11-02 16:25:10 发布
1万+

被折叠的 条评论
为什么被折叠?



