using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace StreamWriter和StreamReader
{
class Program
{
static void Main(string[] args)
{
StreamWriter sw = null;
StreamReader sr = null;
try
{
string path = @"d:\123.txt";
if (!File.Exists(path))
{
FileInfo myInfo = new FileInfo(path);
sw = myInfo.CreateText();
string str = @"好好学习,天天向上";
sw.Write(str);
Console.WriteLine("文件已写入");
}
else
{
sr = new StreamReader(path);
string str = sr.ReadToEnd();
Console.WriteLine("文件内容是:" + str);
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if(sw!=null)sw.Close();//if语句不能省略
if(sr!=null)sr.Close();//if语句不能省略
}
}
}
}
C# StreamWriter和StreamReader
最新推荐文章于 2024-05-18 16:39:08 发布