C#IO(二)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Security.AccessControl; namespace StudyIO { class Program { static void Main(string[] args) { // Directory.CreateDirectory("d:\\ss"); //string[] strs = Directory.GetFiles("d:\\mp3"); //foreach (string str in strs) //{ // Console.WriteLine(str); //} string[] strs = Directory.GetFileSystemEntries("e:\\ftppublish"); foreach(string s in strs) { if (Directory.Exists(s)) { string[] ss = Directory.GetFileSystemEntries(s); foreach(string fs in ss) { Console.WriteLine(fs); } } } } static void Test8() { Stream streams = File.Open("D:\\1.cs", FileMode.Open); Stream streamw = File.Open("D:\\1.cs", FileMode.Open); BufferedStream stream = new BufferedStream(streamw); // stream.Write(); // stream.Read(); } static void Test7() { FileStream stream = File.Open("d:\\1.cs", FileMode.Open); BinaryReader reader = new BinaryReader(stream); byte[] buffer = new byte[1024]; int i = 0; string str = ""; while ((i = reader.Read(buffer, 0, buffer.Length)) != 0) { str += Encoding.UTF8.GetString(buffer, 0, i); } reader.Close(); stream.Close(); Console.WriteLine(str); } static void Test6() { //FileStream stream = File.Open("D:\\1.cs", FileMode.Append); //StreamWriter writer = new StreamWriter(stream); //writer.Write("你好"); //writer.Close(); // Test5(); } static void Test5() { //只是单独操作字符 StreamReader reader = new StreamReader("d:\\1.cs",Encoding.UTF8); //先读 string str = reader.ReadLine(); while (str != null) { Console.WriteLine(str); str = reader.ReadLine(); } reader.Close(); } static void Test4() { //1,读源文件 FileStream read = File.Open(@"E:\FTPPUBLISH\学习资料\[电影天堂-www.dy2018.net]龙门飞甲.720p.HD中文字幕.rmvb", FileMode.Open); long count = read.Length; //2,写文件 FileStream write = File.Open("d:\\23.rmvb", FileMode.OpenOrCreate); byte[] buffer = new byte[1024 * 1024 * 4]; int i = 0; int r = 0; while ((i = read.Read(buffer, 0, buffer.Length)) != 0) { r = r + i; string s = (((double)r / count) * 100).ToString("0.00") + "%"; Console.WriteLine(s); write.Write(buffer, 0, i); } write.Close(); read.Close(); } static void Test3() { FileStream fs = File.Open("d:\\1.cs", FileMode.Open); //默认缓冲区为1024,每次从文件(文件流)读1024个字节 byte[] buffer = new byte[1024]; //当程序读文件的收,把读取的自己从缓冲流的第一个元素开始放。 //已经读取的字节数,如果i=0的话,说明已经读取到末尾了,从而结束循环 int i = 0; string result = ""; while ((i = fs.Read(buffer, 0, 1024)) != 0) { Console.WriteLine(i); result += Encoding.UTF8.GetString(buffer, 0, i); } fs.Close(); Console.WriteLine(result); } static void Test2() { //写操作 FileStream fs = File.Open("d:\\1.cs",FileMode.Open); //向文件中写内容 string str = "这是C#的IO操作"; byte[] buffer = Encoding.UTF8.GetBytes(str); fs.Write(buffer,0,buffer.Length); fs.Close(); } static void Test() { //File.Create("d:\\1.cs"); //File.Copy("d:\\12.mp3", "E:\\刀剑如梦.mp3"); //File.Delete("E:\\刀剑如梦.mp3"); //File.Move("d:\\12.mp3", "E:\\刀剑如梦.mp3"); // FileAttributes fa = File.GetAttributes("E:\\刀剑如梦.mp3"); // Console.WriteLine(fa); // DateTime dt = File.GetCreationTime("E:\\刀剑如梦.mp3"); // Console.WriteLine(dt.ToString()); } } }