namespace Filee
{
class Program
{
static void Main(string[] args)
{
string path=@"c:/users/shenbao/File/shen.txt";
try
{
File.Delete(path);
}
catch (Exception ex)
{
Console.WriteLine("没有当前指定的文件:", ex.ToString());
}
//创建文件或者是覆盖原有文件
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(
"Hello shenbao sdf谌豹ksdfj/nlskdjflsjkdflksdjsdklfjsld/nsdklfjslk"
);
}
}
//读出文件
using (StreamReader sr = File.OpenText(path))
{
string s = string.Empty;
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
try
{
File.Copy(path, @"c:/users/shenbao/File/cb.txt");
File.Delete(path);
}
catch(Exception ex)
{
Console.WriteLine("没有当前指定的文件:", ex.ToString());
}
//流文件的操作
string Path = @"c:/zheng.txt";
FileStream fsr = new FileStream(Path, FileMode.Create);
Byte[] info = new UTF8Encoding(true).GetBytes("This is shenbao's text!!!");
fsr.Write(info, 0, info.Length);
fsr.Close();
using (StreamReader srr = File.OpenText(Path))
{
string strings;
while ((strings = srr.ReadLine()) != null)
{
Console.WriteLine(strings);
}
}
Thread.Sleep(5000);
}
}
}