File和FileInfo的区别总结
2011-04-08 22:28
501 查看
File类提供了创建、删除、复制、移动文件的静态方法
FileInfo类提供了创建、删除、复制、移动文件的实例方法。(不可继承)
File类的方法每次执行都要验证安全机制,所以在少量使用的时候用File效率高,但是如果用的多了 要用FileInfo效率高
string path = @"D:/test.txt";
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("World");
}
using (StreamReader sr = File.OpenText(path))
{
StringBuilder sb = new StringBuilder();
string str = "";
while ((str = sr.ReadLine()) != null)
{
sb.Append(str);
}
}
try
{
string path2 = @"D:/temp.txt";
string pathNew = @"D:/tempNew.txt";
File.Delete(path2);
File.Copy(path2, pathNew);
File.Delete(pathNew);
}
catch (IOException ex)
{
}
}
string path3 = @"D:/net.txt";
if (File.Exists(path3))
{
File.Delete(path);
}
using (FileStream fs = File.Create(path3))
{
byte[] byteData = new UTF8Encoding(true).GetBytes("this is some text in the text");
fs.Write(byteData, 0, byteData.Length);
}
using (StreamReader sr = File.OpenText(path3))
{
StringBuilder sb = new StringBuilder();
string str = "";
while ((str = sr.ReadLine()) != null)
{
sb.Append(str);
}
}
}
FileInfo类提供了创建、删除、复制、移动文件的实例方法。(不可继承)
File类的方法每次执行都要验证安全机制,所以在少量使用的时候用File效率高,但是如果用的多了 要用FileInfo效率高
string path = @"D:/test.txt";
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("World");
}
using (StreamReader sr = File.OpenText(path))
{
StringBuilder sb = new StringBuilder();
string str = "";
while ((str = sr.ReadLine()) != null)
{
sb.Append(str);
}
}
try
{
string path2 = @"D:/temp.txt";
string pathNew = @"D:/tempNew.txt";
File.Delete(path2);
File.Copy(path2, pathNew);
File.Delete(pathNew);
}
catch (IOException ex)
{
}
}
string path3 = @"D:/net.txt";
if (File.Exists(path3))
{
File.Delete(path);
}
using (FileStream fs = File.Create(path3))
{
byte[] byteData = new UTF8Encoding(true).GetBytes("this is some text in the text");
fs.Write(byteData, 0, byteData.Length);
}
using (StreamReader sr = File.OpenText(path3))
{
StringBuilder sb = new StringBuilder();
string str = "";
while ((str = sr.ReadLine()) != null)
{
sb.Append(str);
}
}
}
相关文章推荐
- File,FileInfo;Directory,DirectoyInfo的区别
- File和Directory,FileInfo和DirectoryInfo 之间的区别
- File和FileInfo的区别
- file与fileinfo的区别
- C#中 File,Directory,FileInfo,DirectoryInfo区别与应用
- C#中 File,Directory,FileInfo,DirectoryInfo区别与应用
- File,FileInfo,FileStream,StreamReader的区别与用法
- File和FileInfo的区别
- C#中 File,Directory,FileInfo,DirectoryInfo区别与应用
- C#中 File,Directory,FileInfo,DirectoryInfo区别与应用
- C#中 File,Directory,FileInfo,DirectoryInfo区别与应用
- Java基础回顾--java IO流相关的类的分类和总结 3 【Java File类中list()、listFiles()的使用及区别,认识和使用匿名内部类】
- C#中 File,Directory,FileInfo,DirectoryInfo区别与应用
- File,FileInfo,FileStream,StreamReader的区别与用法
- File,FileInfo,FileStream,StreamReader的区别与用法
- File,FileInfo,FileStream,StreamReader的区别与用法
- FileInfo和file的区别
- 自总结之如何修改指定目录下的指定文件名及File.list()和File.listFiles()的区别
- File类与FileInfo类的区别
- C#文件操作-File类和FileInfo类有什么区别?