码迷,mamicode.com
首页 > 其他好文 > 详细

文件输入输出知识汇总

时间:2015-04-07 17:27:41      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.IO;
  6 
  7 namespace iio
  8 {
  9     class Program
 10     {
 11         static void Main(string[] args)
 12         {
 13             // FileInfo f = new FileInfo("ir.txt");
 14             //// Console.WriteLine(f.Name);
 15 
 16             // //using (FileStream fs = f.Create())
 17             // //{
 18             // //}
 19             // using (StreamWriter sw = f.AppendText())
 20             // {
 21             //     sw.WriteLine("ii");
 22             // }
 23             // //FileStream fi = File.Create("");
 24 
 25             //获取一个FileStream对象
 26             using (FileStream fs = File.Open("one.dat", FileMode.Create))
 27             {
 28                 //把字符串编码成字节数组
 29                 string msg = "Hello";
 30                 byte[] buffer = Encoding.Default.GetBytes(msg);
 31 
 32                 //把byte[]写入文件
 33                 fs.Write(buffer, 0, buffer.Length);
 34 
 35                 //重置内部流的位置
 36                 fs.Position = 0;
 37 
 38                 byte[] rebyte = new byte[buffer.Length];
 39 
 40                 for (int i = 0; i < buffer.Length; i++)
 41                 {
 42                     rebyte[i] = (byte)fs.ReadByte();
 43                     Console.WriteLine(rebyte[i]);
 44 
 45                 }
 46                 Console.WriteLine(Encoding.Default.GetString(rebyte));
 47             }
 48 
 49 
 50             using (StreamWriter sw = File.CreateText("two.txt"))
 51             {
 52                 sw.WriteLine("1");
 53                 sw.WriteLine("2");
 54                 sw.Write("3");
 55                 sw.Write("3");
 56                 sw.Write(sw.NewLine);
 57                 sw.Write(9);
 58             }
 59 
 60             using (StreamReader sr = File.OpenText("two.txt"))
 61             {
 62                 string input = null;
 63                 while ((input = sr.ReadLine()) != null)
 64                 {
 65                     Console.WriteLine(input);
 66                 }
 67             }
 68 
 69 
 70             using (StringWriter sr = new StringWriter())
 71             {
 72                 //sr.GetStringBuilder();
 73 
 74                 sr.WriteLine("ioio");
 75                 StringBuilder bu = sr.GetStringBuilder();
 76                 bu.AppendLine("99");
 77                 //    Console.WriteLine(bu);
 78 
 79                 using (StringReader s = new StringReader(sr.ToString()))
 80                 {
 81                     string input;
 82                     while ((input = s.ReadLine()) != null)
 83                     {
 84                         Console.WriteLine(input);
 85                     }
 86                 }
 87             }
 88 
 89 
 90             FileInfo info = new FileInfo("binfile.txt");
 91 
 92             using (BinaryWriter bw = new BinaryWriter(info.OpenWrite()))
 93             {
 94                 int num = 89090;
 95                 bw.Write(num);
 96                 bw.Write(9.8);
 97             }
 98 
 99             //从流中读取二进制数据
100             using (BinaryReader br = new BinaryReader(info.OpenRead()))
101             {
102                 Console.WriteLine(br.ReadInt32());
103             }
104 
105 
106             //确定指向要观察的目录路径
107             FileSystemWatcher watcher = new FileSystemWatcher();
108             watcher.Path = "folder";
109 
110             //设置需要"留意"的事情
111             watcher.NotifyFilter = NotifyFilters.LastAccess
112                 | NotifyFilters.LastWrite
113                 | NotifyFilters.FileName
114                 | NotifyFilters.DirectoryName;
115 
116 
117             //只观察文本文件
118             watcher.Filter = "*.txt";
119             watcher.Changed += new FileSystemEventHandler(OnChanged);
120 
121             //开始观察目录
122             watcher.EnableRaisingEvents = true;
123 
124 
125 
126             Console.ReadLine();
127         }
128 
129         static void OnChanged(object sender, FileSystemEventArgs e)
130         {
131             Console.WriteLine("Fil:{0} {1}", e.FullPath, e.ChangeType.ToString() == "Changed" ? "修改" : e.ChangeType.ToString());
132         }
133     }
134 }

 

文件输入输出知识汇总

标签:

原文地址:http://www.cnblogs.com/nsky/p/4398722.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!