zoukankan      html  css  js  c++  java
  • c# 第八课 linq stream

    1.对文件和目录的访问

    ①访问目录

    可以实例化DirectoryInfo来对特定的目录进行浏览与访问(注意权限限制),在敲书上的代码时,发现其中有一个变量 indentLevel要是按照书上初始化为-1时无法进行循环输出所访问的目录名及其修改时间,所以我把值初始为1。

    static int dirCounter = 1;
     2          static int indentLevel = 1;
     3          public static void Main()
     4          {
     5              Tester t = new Tester();
     6              //start directory
     7  
     8              string theDirectory = Environment.GetEnvironmentVariable("windir");
     9              DirectoryInfo dir = new DirectoryInfo(theDirectory);
    10              t.ExploreDirectory(dir);
    11              Console.WriteLine("
    
    {0}directory found.
    ", dirCounter);
    12          }
    13 
    14          private void ExploreDirectory(DirectoryInfo dir)
    15          {
    16              //推入一个目录层级
    17              indentLevel++;
    18              //子目录缩进
    19              for(int i = 0; i < indentLevel; i++)
    20              {
    21                  Console.WriteLine(" ");
    22                  Console.WriteLine("[{0}]{1} [{2}]
    ", indentLevel, dir.Name, dir.LastAccessTime);
    23                  DirectoryInfo[] directories = dir.GetDirectories();
    24                  foreach(DirectoryInfo direc in directories)
    25                  {
    26                      dirCounter++;
    27                      ExploreDirectory(direc);//递归调用自己
    28                  }
    29                  indentLevel--;
    30              }
    31 
    32          }
    

     部分结果为

    也可以用Directory.GetCurrentDiectory()来得到当前目录(directory类的方法),我觉得这个方法会很常用。

    流用于对IO处理,在System.IO名称空间中有以下类:   BinaryReader/Writer   TextReader/Writer   Stream 其中类Stream为抽象类。由此有三个派生类:   MemoryStream:对内存进行读取与写入   BufferedStream:对缓冲器进行读取/写入   FileStream:对文件执行读取与写入

    TextReader/Writer为抽象类。由此派生类: StreamReader/StreamWirter StringReader/StreamWrite

    IO操作基本上需要用到Stream相关的子类。其实对于Stream来说,操作起来比较简单,只要对细节的处理稍微注意一下,相信在使用它的时候也可以得心应手。

    static void Main(string[] args)
    {
    //文件流的使用:
        Console.WriteLine("请输入一个路径:");
        string s=Console.ReadLine(); 
        FileStream fileStream=new FileStream(s,FileMode.OpenOrCreate); 
        BinaryWriter binStream=new BinaryWriter (fileStream); 
        for(int i=1;i<=10;i++) 
        {
            binStream.Write((int)i); //将 4 字节有符号整数写入当前流,并将流的位置提升 4 个字节。
        } 
        binStream.Close(); 
        fileStream.Close(); 
        FileStream f=new             FileStream(s,FileMode.Open,FileAccess.Read,FileShare.ReadWrite); 
        BinaryReader buf=new BinaryReader(f); 
        for(int i=1;i<=10;i++) 
        {
        Console.WriteLine("输出{0}",buf.ReadInt32()); //这样是可以的,因为int是32位的
        }
    }   
    

     输出结果为

    关于file和directory:

    1. Classes provided for file and directory manipulation.

    2. The classes you need are in the System.IO namespace.

    3. File class: Represents a file on disk.

    4. Directory class: Represents a directory (Windows folder).

    继承此类的对象可以跨越应用程序域边界被引用,甚至被远程引用。远程调用时, 将产生一个远程对象在本地的透明代理,通过此代理来进行远程调用。 使用代理交换消息来跨应用程序域边界进行通讯的对象的基类。

    Working with Directories:

    1. The Directory class exposes static methods for creating, moving, and exploring directories.

    2. All the methods of the Directory class are static; therefore, you can call them all without having an instance of the class.

    3. The DirectoryInfo class is a similar class, but one that has nothing but instance members (i.e.,no static members at all).

    4. The FileSystemInfo class has a number of properties and methods that provide information about a file or directory.

    Creating a DirectoryInfo:

    1. Object To explore a directory hierarchy, you need to instantiate a DirectoryInfo object.

    2. The DirectoryInfo class provides methods for getting not just the names of contained files and directories, but also FileInfo and DirectoryInfo objects, allowing you to dive in to the hierarchical structure, extracting subdirectories and exploring these recursively.

    Working with Files:

    1. The DirectoryInfo object can also return a collection of all the files in each subdirectory found.

    2. The DirectoryInfo.GetFiles( ) method returns an array of FileInfo objects, each of which describes a file in that directory.

    3. The FileInfo and File objects relate to one another, much as DirectoryInfo and Directory do.

    4. Like the methods of Directory, all the File methods are static.

    5. Like DirectoryInfo, all the methods of FileInfo are instance methods.

  • 相关阅读:
    方法
    属性
    Cocoapods完整使用篇
    这样好用的ReactiveCocoa,根本停不下来【转载】
    OS开发之旅之App的生命周期【转载】
    ios 推送证书没有密钥 解决方案【转载】
    移动应用开发测试工具Bugtags集成和使用教程【转载】
    MagicalRecord使用教程【转载】
    GIT客户端的使用【原创】
    iOS开发系列--让你的应用“动”起来【转载】
  • 原文地址:https://www.cnblogs.com/GSONG/p/4495539.html
Copyright © 2011-2022 走看看