C#IO

I/O:input/output输入和输出
  参照物:程序
  输入:值得是通过外接设备向程序输入
  输出:程序箱外界设备输出内容

看段代码慢慢理解吧:

View Code
 1 using System.IO;
 2 using System.Security.AccessControl;
 3 
 4 namespace StudyIO
 5 {
 6    public  class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             //1、读取源文件
11             FileStream read = File.Open(@"F:\视频\轩辕剑之天之痕13.HDTV.rmvb",FileMode.Open);
12             long count = read.Length;
13             //2、写入内容
14             FileStream write = File.Open("d:\\轩辕剑之天之痕13.HDTV.rmvb",FileMode.OpenOrCreate);
15             byte[] by = new byte[1024*1024*8];
16             int n = 0;
17             int r = 0;
18             while ((n = read.Read(by,0,by.Length))!=0)
19             {
20                 r = r + n;
21                 string str = (((double)r/count)*100).ToString("0.00")+"%";
22                 Console.WriteLine(str);
23                 write.Write(by,0,n);                
24                 
25             }           
26            write.Close();         
27             read.Close();
28             //3、删除源文件           
29             File.Delete(@"F:\视频\轩辕剑之天之痕13.HDTV.rmvb");
30            
31            
32         }
33         static void Test2() 
34         {
35             if (File.Exists("f:\\我的最爱.mp3"))
36             {
37                 Console.WriteLine(" 这就当我手机铃声吧?");
38             }
39             FileStream fs = File.Open("f:\\7.9.txt", FileMode.Open);//先打开文件
40             byte[] by = new byte[2048];
41             int n = 0;
42             string s = "";
43             while ((n = fs.Read(by, 0, by.Length)) != 0)   //从by中读取,第0个元素,读取by.Length个长度
44             {
45                 s += Encoding.UTF8.GetString(by, 0, n);
46             }
47             fs.Close();
48             Console.WriteLine(s);
49         }
50         static void Test() 
51         {
52             File.Create("d:\\1.txt");//创建文件
53             //File.Copy("f:\\滴答滴.mp3","d:\\我的最爱.mp3");//复制文件
54             //File.Delete("d:\\我的最爱1.mp3");//删除文件
55             //File.Move("d:\\我的最爱.mp3", "f:\\我的最爱.mp3");//移动相当于剪切
56             FileAttributes fs = File.GetAttributes("f:\\我的最爱.mp3");
57             Console.WriteLine(fs);
58             DateTime dt = File.GetCreationTime("f:\\我的最爱.mp3"); //返回指定文件或目录的创建日期和时间。
59             Console.WriteLine(dt.ToString());
60         }
61     }
62 }

慢慢领会其中的奥妙....慢慢成长!

posted @ 2012-08-04 14:31  妍珊  阅读(1932)  评论(0编辑  收藏  举报