using System; using System.IO; namespace _31_file类 { class Program { static void Main(string[] args) { File.Create(@"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\02--面向对象继承\02\video\new.txt"); Console.WriteLine("创建成功"); } } }
创建一个文件 File.Create()
File.Create(@"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\02--面向对象继承\02\video\new.txt");
删除一个文件File. Delete()
File.Delete(@"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\02--面向对象继承\02\video\new.txt");
复制一个文件 File.Copy(file,new_file)
File.Copy(@"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\02--面向对象继承\01\资料.rar", @"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\02--面向对象继承\02\video\资料.rar");
读数据 File.ReadAllBytes()
byte[] buffer = File.ReadAllBytes(@"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\03--面向对象多态\01\video\new.txt"); string s = Encoding.GetEncoding("UTF-8").GetString(buffer); Console.WriteLine(s);
写入数据 File.WriteAllBytes(file,data)
string s = "hello Irving"; byte[] buffer = Encoding.Default.GetBytes(s); File.WriteAllBytes(@"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\03--面向对象多态\01\video\new.txt", buffer );
读取文件为数组 File.ReadAllLines(file, Encoding)
string[] s = File.ReadAllLines(@"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\03--面向对象多态\01\video\new.txt", Encoding.Default); Console.WriteLine(s); foreach (var item in s) { Console.WriteLine(item); }
数组格式的数据保存文件 File.WriteAllLines(file, new string[]{})
File.WriteAllLines(@"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\03--面向对象多态\01\video\new.txt", new string[] { "dsfsdf", "hello" });
读取文件为文本 File.ReadAllText(file, Encoding)
string s = File.ReadAllText(@"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\03--面向对象多态\01\video\new.txt", Encoding.Default); Console.WriteLine(s);
文本格式的数据保存文件 File.WriteAllText(file, text)
File.WriteAllText(@"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\03--面向对象多态\01\video\new.txt", "fhgkfdshgk");
追加文本格式数据
File.AppendAllText(@"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\03--面向对象多态\01\video\new.txt", "\nhello");
fileStream 读取文件
using System; using System.IO; using System.Text; namespace _32_FileStream { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); // FileStream 操作字节的 // StreamReader和StreamWriter 是操作字符的 FileStream fsRead = new FileStream(@"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\03--面向对象多态\01\video\new.txt", FileMode.OpenOrCreate, FileAccess.Read); byte[] buffer = new byte[1024*1024*5]; // 用于存放字节数据 int r = fsRead.Read(buffer,0,buffer.Length); // 返回实际读取的有效字节数 string s = Encoding.Default.GetString(buffer, 0, r); // 字节转字符串 Console.WriteLine(s); // 释放所有占用资源 fsRead.Close(); fsRead.Dispose(); } } }
fileStream 写入文件
using (FileStream fs = new FileStream(@"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\03--面向对象多态\01\video\new.txt", FileMode.OpenOrCreate, FileAccess.Write)) { string s = "我来覆盖你"; byte[] buffer = Encoding.Default.GetBytes(s); fs.Write(buffer, 0, buffer.Length); }
文件复制
using System; using System.IO; using System.Text; namespace _32_FileStream { class Program { static void Main(string[] args) { string source = @"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\03--面向对象多态\01\video\new.txt"; string target = @"F:\BaiduNetdiskDownload\01.Net基础教程\01.Net基础\03-c#高级\03--面向对象多态\01\video\new.bak.txt"; CopyFile(source, target); } public static void CopyFile(string source, string target) { // 创建一个负责读取的流 using (FileStream fs = new FileStream(source, FileMode.Open, FileAccess.Read)) { using (FileStream fs_write = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write)) { int size = 0; while (true) { byte[] buffer = new byte[1024 * 1024 * 5]; int r = fs.Read(buffer, 0, buffer.Length); if (r==0) return; fs_write.Write(buffer, 0, r); size += r; } } } } } }
//StreamReader using (StreamReader sr = new StreamReader(source, Encoding.Default)) { while (!sr.EndOfStream) { Console.WriteLine(sr.ReadLine()); Console.WriteLine(sr.Read()); } }
using (StreamWriter sw = new StreamWriter(source, true)) { sw.Write("山东i过后四大公害世噶啥的回\n复收到回复\n"); //sw.WriteL("山东i过后四大公害世噶啥的回复收到回复"); sw.WriteLine("山东i过后四大公害世噶啥的回复收到回复"); }