黑马程序员——File笔记读,写,复制
#region ReadAllBytes
byte[] buffer = File.ReadAllBytes(@"C:\Users\dell\Desktop\新建文件夹.txt");
//将字节数组转换成字符串
//解决乱码 GB2312 GBK Default UTF8
//string str = System.Text.Encoding.UTF8.GetString(buffer);
//Console.WriteLine(str);
//Console.ReadKey();
#endregion
#region ReadAllLines
//string[] str = File.ReadAllLines(@"C:\Users\dell\Desktop\新建文件夹.txt");
//for (int i = 0; i < str.Length; i++)
//{
// Console.WriteLine(str[i]);
//}
//Console.ReadKey();
#endregion
#region ReadAllText
//string str = File.ReadAllText(@"C:\Users\dell\Desktop\新建文件夹.txt");
//Console.WriteLine(str);
//Console.ReadKey();
#endregion
//复制一个多媒体文件
#region WriteAllBytes
//先读取 再写入
//byte[] buffer = File.ReadAllBytes(@"C:\Users\dell\Desktop\好声音.wmv");
//File.WriteAllBytes(@"C:\Users\dell\Desktop\好声音复制.wmv", buffer);
//Console.WriteLine("复制成功");
//Console.ReadKey();
#endregion
#region WriteAllLines
//File.WriteAllLines(@"C:\Users\dell\Desktop\新的.txt", new string[] { "1", "2", "3", "4" });
//Console.WriteLine("写入成功");
//Console.ReadKey();
#endregion
#region WriteAllText
//File.WriteAllText(@"C:\Users\dell\Desktop\新的.txt", "看有没有覆盖掉");
//Console.WriteLine("写入成功");
//Console.ReadKey();
#endregion
File.AppendAllText(@"C:\Users\dell\Desktop\新的123.txt","哈哈哈哈哈");
Console.ReadKey();
最强大的ReadAllBytes和WriteAllBytes 转换成2进制数据进行读写
File只适用与小型的文件 代码简洁省时 大文件用FileStream
posted on 2016-07-15 10:51 张亮13128600812 阅读(182) 评论(0) 编辑 收藏 举报