使用File和FileInfo类操作文件
2010-08-08 01:30
417 查看
.NET Framework类库包含用于处理文件的两个类似的类:FileInfo和File.
File类为创建、删除和操作文件提供静态方法,而FileInfo类为文件操作提供实例方法。类似与Directory类,File类只提供静态方法,而没有包含任何特性。
File类包含用于将内容写入文件的4种方法:
WriteAllText():创建文本,将字符串写入该文件,关闭文件
AppendAllText():将字符串附加到已有的文件
WriteAllLines():创建文件,将字符串数组写入该文件,关闭文件
WriteAllBytes():创建文件,将字节数组写入该文件,关闭文件
下面的代码显示了如何使用各种方法将一些内容写入文件:
File类也包含用于从文件读取内容的3种方法:
ReadAllText():打开文件,读取该文件中的所有文本并放入字符串中,关闭文件
ReadAllLines():打开文件,读取该文件中的所有文本并放入字符串数组中,关闭文件
ReadAllBytes():打开文件,读取该文件中的所有文本并放入字节数组中,关闭文件
File类为创建、删除和操作文件提供静态方法,而FileInfo类为文件操作提供实例方法。类似与Directory类,File类只提供静态方法,而没有包含任何特性。
using System; using System.IO; namespace ConsoleApplicationFile { class Program { static void Main(string[] args) { string filePath = @"D:/temp/textfile.txt"; string fileCopyPath = @"D:/temp/textfile_copy.txt"; string newFileName = @"D:/temp/textfile_newcopy.txt"; try { //---if file already existed--- if (File.Exists(filePath)) { //---delete the file--- File.Delete(filePath); } //---create the file again FileStream fs = File.Create(filePath); fs.Close(); //---make a copy of the file--- File.Copy(filePath, fileCopyPath); //---rename the file--- File.Move(fileCopyPath, newFileName); //---display the creation time--- Console.WriteLine(File.GetCreationTime(newFileName)); //---make the file read-only and hidden--- File.SetAttributes(newFileName, FileAttributes.ReadOnly); File.SetAttributes(newFileName, FileAttributes.Hidden); } catch (IOException ex) { Console.WriteLine(ex.Message); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); } } }
File类包含用于将内容写入文件的4种方法:
WriteAllText():创建文本,将字符串写入该文件,关闭文件
AppendAllText():将字符串附加到已有的文件
WriteAllLines():创建文件,将字符串数组写入该文件,关闭文件
WriteAllBytes():创建文件,将字节数组写入该文件,关闭文件
下面的代码显示了如何使用各种方法将一些内容写入文件:
using System; using System.IO; using System.Text; namespace ConsoleApplicationFile { class Program { static void Main(string[] args) { string filePath = @"D:/temp/textfile.txt"; string strTextToWrite = "This is a string"; string[] strLinesToWrite = new string[] { "Line1","Line2"}; byte[] bytesToWrite = ASCIIEncoding.ASCII.GetBytes("This is a string"); try { File.WriteAllText(filePath, strTextToWrite); File.AppendAllText(filePath, strTextToWrite); File.WriteAllLines(filePath, strLinesToWrite); File.WriteAllBytes(filePath, bytesToWrite); } catch (IOException ex) { Console.WriteLine(ex.Message); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); } } }
File类也包含用于从文件读取内容的3种方法:
ReadAllText():打开文件,读取该文件中的所有文本并放入字符串中,关闭文件
ReadAllLines():打开文件,读取该文件中的所有文本并放入字符串数组中,关闭文件
ReadAllBytes():打开文件,读取该文件中的所有文本并放入字节数组中,关闭文件
相关文章推荐
- 使用VB.NET的FileInfo和DirectoryInfo类进行文件操作
- Java编程那些事儿86——文件操作之File类使用
- vb.net 教程 4-3 文件操作 FileInfo 1
- C#的FileInfo类实现文件操作实例
- 收藏——在C#使用SHGetFileInfo获取(管理)文件或者文件夹图标(C#封装Win32函数的一个例子)
- 使用Windows的SHFileOperation外壳函数实现文件操作
- vb.net 教程 4-3 文件操作 FileInfo 2
- JAVA资格认证:文件操作之File类使用
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- java文件操作笔记(File类的使用)
- C#获取文件(磁盘驱动器)的关联图标(使用API SHGetFileInfo)
- ini 文件操作记要(2): 使用 TMemIniFile
- Java编程那些事儿86——文件操作之File类使用
- CodeProject - 在C#使用SHGetFileInfo获取(管理)文件或者文件夹图标(C#封装Win32函数的一个例子)
- 获取Exe文件版本信息的函数(使用GetFileVersionInfo得到TFileVersionInfo结构体,包含12项内容)
- Xml文件操作和文件操作(FileInfo类)
- Windows下使用脚本语言操作文件系统之五-file对象
- C使用FILE指针文件操作
- Python使用os模块和fileinput模块来操作文件目录
- 生动讲解使用不同方式操作File文件的方法之间的差异