Stream

1.对文件和目录的访问

①访问目录

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

②对文件的访问与读写

书中没有具体提及,所以百度了一下,= =

c#中对txt文件的读取有两种方式:使用FileStream类进行文件的读取。

                                            使用StreamReader读取文件。

对txt文件的写入也有两种方式:使用FileStream类创建文件,然后将数据写入到文件。

                                         使用FileStream类创建文件,使用StreamWriter类,将数据写入到文件。

首先是对文件的读取。

文件名为“text.txt”,内容为:

”stream ,directory, fileinfo, directoryinfo,write sth into the new file.
文件,目录读取。“

法1:

(注意 Encoding.Default.GetDecoder() 中encoding后面是字符编码,ASCII,unicode等)

 1 try
 2              {
 3                  byte[] byData = new byte[100];
 4                  char[] charData = new char[1000];
 5                  FileStream file = new FileStream(@"D:大二下C#blogstream	ext.txt", FileMode.Open);
 6                  file.Seek(0, SeekOrigin.Begin);
 7                  file.Read(byData, 0, 100);
 8                  Decoder d = Encoding.Default.GetDecoder();
 9                  d.GetChars(byData, 0, byData.Length, charData, 0);
10                  Console.WriteLine(charData);
11                  file.Close();
12              }
13              catch (IOException e)
14              {
15                  Console.WriteLine(e.ToString());
16              }

结果:

法2:

1          public static void Main()
2          {
3              StreamReader sr = new StreamReader(@"D:大二下C#blogstream	ext.txt");
4              String line;
5              while ((line = sr.ReadLine()) != null)
6              {
7                  Console.WriteLine(line.ToString());
8              }
9          }

结果为:

(可能因为默认字符编码问题。。。中文读出来的只有问号= =!)。

文件的写入与读取相似,注意文件写权限。

2.网络I/O

因为在计算机网络上学了关于TCP,UDP,端口,IP地址等等的知识,看到这章里面有自己搭建server和client并进行数据读写的时候,就试了试。(发现其实模拟服务器与主机间的交互也不是很难。)

首先是server端代码:

 1  public class NetworkIOServer
 2     {
 3         public static void Main()
 4         {
 5             NetworkIOServer app = new NetworkIOServer();
 6             app.Run();
 7         }
 8         private void Run()
 9         {
10             IPAddress add = IPAddress.Parse("127.0.0.1");
11             TcpListener tl = new TcpListener(add, 65000);
12             tl.Start();
13 
14             for(;;)
15             {
16                 Socket so = tl.AcceptSocket();
17                 Console.WriteLine("client connected");
18 
19                 send(so);
20                 Console.WriteLine("disconnected");
21                 so.Close();
22                 Console.WriteLine("exist!");
23                 break;
24             }
25         }
26         private void send(Socket so)
27         {
28             NetworkStream ns = new NetworkStream(so);
29             StreamWriter sw = new StreamWriter(ns);
30             StreamReader sd = new StreamReader("text.txt");
31             string str;
32             do
33             {
34                 str = sd.ReadLine();
35                 if (str != null)
36                 {
37                     Console.WriteLine("sending {0}", str);
38                     sw.WriteLine(str);
39                     sw.Flush();
40                 }
41             }
42             while (str != null);
43             ns.Close();
44             sw.Close();
45             sd.Close();
46         }
47     }

然后是client端代码:

 1 public class Client
 2     {
 3         static void Main(string[] args)
 4         {
 5             TcpClient sfs;
 6             try
 7             {
 8                 sfs = new TcpClient("localHost", 65000);
 9             }
10             catch
11             {
12                 Console.WriteLine("failed to connected 65000");
13                 return;
14             }
15             NetworkStream ns = sfs.GetStream();
16             StreamReader sd = new StreamReader(ns);
17             try
18             {
19                 string output;
20                 do
21                 {
22                     output = sd.ReadLine();
23                     if (output != null)
24                     {
25                         Console.WriteLine(output);
26                     }
27 
28                 }
29                 while (output != null);
30             }
31             catch
32             {
33                 Console.WriteLine("exception reading!");
34             }
35             ns.Close();
36         }
37     }

其中text.txt内容为:”c#,network IO programing, interesting thing!“

先运行server端,再运行client端(注意二者顺序不能颠倒),建立TCP连接后两者分别的控制台输出为:

server:

client:

over

原文地址:https://www.cnblogs.com/3013218071zjr/p/4483326.html